From: Brian Ross
Date: 2008-08-29T02:12:07+09:00
Subject: Writing a method to handle a code block?
------=_Part_7767_1479113.1219943808313
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
From Beginning Ruby:
def each_vowel(&code_block)
%w{a e i o u}.each { |vowel| code_block.call(vowel) }
end
each_vowel { |vowel| puts vowel }
I am trying to figure out how that works but I'm still having a bit of
trouble. Could someone break it down bit by bit to show what it's doing?
def each_vowel(&code_block)
It defines a method that takes a code block (is the & necessary?). What does
it mean to have a method that takes a code block?
%w{a e i o u}.each { |vowel| code_block.call(vowel) }
Then it takes an array of vowels, which call the each method to pass each
one into the following block through |vowel| as a block argument. The block
arguments are then called by the variable code_block (I don't understand
this).
Brian
------=_Part_7767_1479113.1219943808313--