2008年5月1日 星期四

block, proc and &

call method with block parameter:
two syntaxes:
1.  {   } 
     this is useful for one line
2. do   end
    this is useful for multiple line
ex:
["a", "b"].each do |i|
    puts "#{i}"
end
the element between |  | refers to each element in the array  

define a method with block parameter
use &
ex:
class Array
     def test( &arg)
           find_all( &arg)
     end    
end

pass proc parameter to a method received block
ex:
a=lambda{ puts "abc"}
test(&a)

沒有留言: