Ita*_*vka 3 ruby ruby-on-rails
通过代码块我的意思是:
def callBlock
yield
yield
end
callBlock { puts "In the block" } #this is the block
Run Code Online (Sandbox Code Playgroud)
b = lambda { puts "this is the block" }
callBlock &b
Run Code Online (Sandbox Code Playgroud)
要么
b.call
Run Code Online (Sandbox Code Playgroud)
带参数:
def call_block_name
yield "Dave"
yield "Mary"
end
b = lambda { |s| puts "this is block #{s}" }
call_block_names &b
Run Code Online (Sandbox Code Playgroud)
和
b.call("sam")
Run Code Online (Sandbox Code Playgroud)