Calling/applying lambda vs. function call - the syntax in Ruby is different. Why?

爷,独闯天下 提交于 2019-12-02 17:59:11

Because in Ruby, methods are not lambdas (like, for example, in JavaScript).

Methods always belong to objects, can be inherited (by sub-classing or mixins), can be overwritten in an object's eigenclass and can be given a block (which is a lambda). They have their own scope for variables. Example method definition:

a = :some_variable
def some_method
  # do something, but not possible to access local variable a
end

# call with:
some_method

However lambdas/procs are plain closures, maybe stored in a variable - nothing else:

a = :some_variable
some_lambda = lambda{
  # do something, access local variable a if you want to
}

# call with:
some_lambda[]

Ruby combines both approaches with a powerful syntax, for example, passing blocks:

def some_method_with_block(a)
  # do something, call given block (which is a lambda) with:
  yield(a) ? 42 : 21
end

# example call:
some_method_with_block(1) do |x|
  x.odd?
end #=> 42

Regular Ruby method calls use () not curly braces which are for blocks. If you don't like [] for calling a lambda, you can always use the call method.

Example:

>> by_two = lambda { |x| x * 2 } #=> #<Proc:0x0000000101304588@(irb):1>
>> by_two[5] #=> 10
>> by_two.call(5) #=> 10

Edit

In newer version of Ruby also:

>> by_two.(5) #=> 10

As to why you can't just do by_two(5), when Ruby sees a bareword it first tries to resolve it as a local variable and if that fails as a method.

If you want brackets, you can do

by_two = lambda { |x| x * 2 }
by_two.(5) # => 10

Note the . between by_two and (5).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!