2008年3月12日 星期三

code block

two syntaxes
ex: (1) { puts "hello" }
(2) do
puts "hello"
end

define a method with call block
ex: def hello()
yield
end
yield will run the program in call block

call a method with call block
ex: hello { puts "hello" }

call a method with call block and arguments
ex: hello ("test", "good") {puts "hello"}

passing data to call block
ex:
method definition:
def hello()
yield "test", "good"
end
call method:
hello { | one, two | puts one + " "+ two }

call block with loop:
ex: ["test1", "test2"].each{ |value| puts value}

沒有留言: