From: Russell Me Date: 2008-02-09T14:20:30+09:00 Subject: Re: info on block arguments Day wrote: > On Feb 8, 2008 10:22 PM, Russell Me wrote: >> So, after 3 days I still don't understand this. Is it really that >> difficult, or am i just plain dumb? > > It is not exactly a simple concept. Especially, I think, if you're not > familiar with object oriented thinking. > >> While I'm looking to understand the kitty_toys sort, it's really just >> because I want to know how it all works. > > I think, actually, that _why's example is pretty complex, so maybe it > would be better for stick with your 3.times example. > >> even this: >> 3.times {|x| puts x } >> >> I see it work, I understand what's going on, but I don't know HOW it's >> working. > > Actually. Maybe let's make it simpler. Forget, for the moment, about > the |x| bit. Let's look at this code: > > 3.times { puts "Arr!" } # Very high seas. > > The block, there, is sort of where you store code for a second. So you > wrap up something to be executed (the puts statement in this case) in > a block and you hand it to the times method. Not every method can > accept a block like that, but this one can. > > So what the times method does is it executes whatever's in the block > it got handed a number of times equal to the integer it was called on > (three in this case). It sort of says, in English: > Do 'puts "Arr!"' three times. > > If you're entirely new to OOP, it may be hard to remember or envision > this, but: The code does not execute in sequence. Remember that 3 is > an object. It is doing something (the method). It can do that thing, > on stuff that you hand it (a block in this case, but this is true for > all arguments). Really, the key difference between a block and any > other argument you might pass to a method is that a block is code that > can be executed, but the execution is held off until the method it's > passed to asks it to execute. > > Does that make some sense? If you get that alright, then I think it'll > be a bit easier to move on to the more complicated examples. Kudos, by > the way, for your perseverence. Lesser men would have given up in > frustration by now. I hope you don't feel like you're being badgered > or yelled at or ganged up on. Hang in there. You'll get it. > > > Ben Ben, that actually was helpful. Understanding that the crap inside the block gets executed 3 times. that makes sense. the kitty toys thing is simply what why threw at me (the reader). too complicated? perhaps. it's the block itself then i don't get. everything else i understand, for the most part. I appreciate the help (and attempts at help) from everyone. this seems to be a very friendly group. This seems such a trivial simple thing, but i can't wrap my little brain around it. -- Posted via http://www.ruby-forum.com/.