如何定义一次代码块多次使用?

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)

Mar*_*usQ 6

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)