Back

Block basic in Ruby( Block 基础 )

发布时间: 2014-01-05 00:16:00

以前虽然学习过block, proc, lambda, 但是一直没有大规模的使用。最近的项目让我不得不用到这个技术。但是遇到些问题。翻了书才知道原委。 (I have to review the 'block in Ruby' because of one of my projects)

一个例子: (a concise example)

# no &param in this method declaration because of the "yield"
# 这个方法的参数声明中,没有 &param 。因为方法体中已经有了 "yield"
def math(a, b)  
    yield a,b 
end

def teach_match a, b, &operator  
  puts " let's do the math: "
  puts math a, b, &operator
end

teach_match(3, 5){ |x, y| x * y}  # => 15

Back