Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am kinda new to Ruby and still trying to understand some of the language design principles. IF I've got it right, the lambda expression call in Ruby must be with square braces, while the "regular" function call is with "regular"/round braces.

Is there a special reason that the syntax is different? Or, in other words, (why) should the caller be aware whether they call a function or apply a lambda expression?

like image 429
BreakPhreak Avatar asked Jun 14 '11 12:06

BreakPhreak


People also ask

How do you call a lambda function in Ruby?

With Ruby, the lambda keyword is used to create a lambda function. It requires a block and can define zero or more parameters. You call the resulting lambda function by using the call method. The literal operator is succinct and commonly used.

What is the difference between proc and lambda in Ruby?

In Ruby, a lambda is an object similar to a proc. Unlike a proc, a lambda requires a specific number of arguments passed to it, and it return s to its calling method rather than returning immediately. proc_demo = Proc.

Can you call a function in a lambda?

The whole lambda function lambda x : x * x is assigned to a variable square in order to call it like a named function. The variable name becomes the function name so that We can call it as a regular function, as shown below. The expression does not need to always return a value.

What is -> in Ruby?

-> == Dash RocketUsed to define a lambda literal in Ruby 1.9.


2 Answers

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.

like image 115
Michael Kohl Avatar answered Oct 11 '22 23:10

Michael Kohl


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
like image 35
J-_-L Avatar answered Oct 11 '22 21:10

J-_-L