From: Siep Korteling Date: 2008-02-07T20:42:36+09:00 Subject: Re: info on block arguments Russell Me wrote: > I'm trying to pick up ruby and I'm impressed by all the cool stuff it > does out of the box. I'm reading a few books but i'm finding why's > poignant guide to ruby to be the most helpful and entertaining > (http://poignantguide.net/ruby). > > However, I'm a bit stuck understanding how block arguments work. in his > example: > > kitty_toys = [ > {:shape => 'sock', :fabric => 'cashmere'}, > {:shape => 'mouse', :fabric => 'calico'}, > {:shape => 'eggroll', :fabric => 'chenille'} > ] > > kitty_toys.sort_by { |toy| toy[:shape] }.each do |toy| > puts "Blixy has a #{ toy[:shape] } made of #{ toy[:fabric] }" > end > > I know what it does. I put it through irb and it works. But i don't > understand HOW it works, exactly. Mainly, the toy bit. I don't quite get > what role it plays in the code and how the code uses it. > > I've read up online and in both books I have, but, for whatever reason i > STILL cannot grasp block arguments and what role they play. > > Maybe I'm just brain farting here, but if anyone could lend some tips on > this I'd appreciate it. I think of a block as Cookiemonster from Sesame street. When you throw something at him, he will name it "cookie" and then eat it. In this case you throw each element of kitty_toys, the block will call them all "toy" and process them. -- Posted via http://www.ruby-forum.com/.