Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between lambda and def method_name in Ruby

I am reading along in pickaxe 1.9 and the author uses lambda like this:

bo = lambda {|param| puts "You called me with #{param}"}
bo.call 99     => 'You called me with 99'
bo.call "cat"  => 'You called me with cat'

My question is this: How is this any better/worse/different than just defining a method that does that same thing? Like so:

def bo(param)
  puts "You called me with #{param}"
end

bo("hello") => 'You called me with hello'

To me the lambda syntax seems much more confusing and spaghetti-like.

like image 265
Josh Avatar asked Apr 18 '12 20:04

Josh


People also ask

What is difference between lambda and Proc 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. new { return "Only I print!" }

What does lambda do in Ruby?

You can run Ruby code in AWS Lambda. Lambda provides runtimes for Ruby that run your code to process events. Your code runs in an environment that includes the AWS SDK for Ruby, with credentials from an AWS Identity and Access Management (IAM) role that you manage.

What is the difference between procs and blocks?

When using parameters prefixed with ampersands, passing a block to a method results in a proc in the method's context. Procs behave like blocks, but they can be stored in a variable. Lambdas are procs that behave like methods, meaning they enforce arity and return as methods instead of in their parent scope.

What does &: mean in Ruby?

In a method argument list, the & operator takes its operand, converts it to a Proc object if it isn't already (by calling to_proc on it) and passes it to the method as if a block had been used.


2 Answers

Lambdas:

  • are variations of Procs,
  • can be converted to/from blocks,
  • do not begin a new closure scope (meaning you can access variables defined outside the scope of your lambda, unlike def),
  • can be passed around as variables.

I recommend checking out this article that explain procs, blocks, and lambdas.

Edit: This link is outdated. For future reference, try this article

like image 77
Matt Huggins Avatar answered Oct 09 '22 18:10

Matt Huggins


The advantage that defining a lambda gives you is that you can then pass that lambda object as an attribute to another method.

def method1 &b
  #... some code
  b.call
end

def method2 &b
  #... some more code...
  b.call
end

def method3 &b
  b.call
  #even more code here
end

myCallback = lambda { "this is a callback that can be called from several methods"}

You can then use it like this:

method1 &myCallback
method2 &myCallback
method3 &myCallback

And the beauty of this, is that you only wrote of code of the callback once, but used it 3 times....

I would recommend you take a look at this link for further reading :)

like image 40
Deleteman Avatar answered Oct 09 '22 16:10

Deleteman