B
Brian Ross
[Note: parts of this message were removed to make it a legal post.]
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
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