When to use lambda in Ruby on Rails?

When should lambda or proc be used? I saw them as anonymous functions, but I'm struggling to understand this concept. I would appreciate any links or examples of when you can use them in Ruby, but especially in Ruby on Rails.

+42
ruby ruby-on-rails
Aug 05 '09 at 12:05
source share
6 answers

http://augustl.com/blog/2008/procs_blocks_and_anonymous_functions/ has an abbreviation for those / procs / lambdas blocks, how you can use them and how they are compared to functions in other languages. This definitely answers your question.

Remember that the last section of the β€œNote on Lambdas” mentions a point that is only valid in Ruby 1.8 and changed in version 1.9 - Ruby: Proc.new {'waffles'} vs. proc {'waffles'}

+39
Aug 05 '09 at 12:23
source share

I do not see where you are distinguishing between Ruby on Rails and Ruby. If you are writing a Ruby on Rails application, you are writing Ruby code, so if it is useful in Ruby, it should be useful in Ruby on Rails.

In any case, this article, Some useful closures in Ruby , should be useful as well: http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/

+9
Aug 05 '09 at 12:29
source share

This is a piece of code that allows you to get through.

This is especially useful in named_scope, it allows you to do something like this:

named_scope :scoped_by_user, lambda {|user| {:conditions=>{:user_id=>user.id}}} 

Say that you have a project model, and you want to get all the projects for one specific user, you can do something like this:

 Project.scoped_by_user(123) 
+6
Aug 7 '09 at 3:51
source share

Where I saw that Lambda is used in testing ...

 lambda do post :create, :user => @attr end.should_not change(User, :count) 

Lambda allows you to run this test at the end to make sure that the code executed in the lambda block does not change the user value.

+5
Jan 17 '11 at 17:35
source share

What is lambda?

Try to do this with irb .

 lam = lambda { puts "Hello world"} lam.class #output is => Proc 

lambda in ruby ​​is also an instance of the Proc class. lambdas are a different flavor of procs.

What is a Proc?

Proc objects are blocks of code that are bound to a set of local variables.

 proc = Proc.new { puts "Hello World" } proc.call #output is => Hello World 

What is the difference between proc and lambda? Comparison will explain usage

Lambdas checks the number of arguments, but procs does not.

 multiply = lambda { |x,y| x*y } multiply.call(2,3) #=>6 multiply.call(2) #ArgumentError: wrong number of arguments (1 for 2) multiply = Proc.new {|x| x*x} multiply.call(2) # =>4 multiply.call(2,3) # =>4 (It ignore the second argument) 

Lambdas and procs treat the return keyword differently (For example, read the article below)

Read this wonderful article for more details http://awaxman11.imtqy.com/blog/2013/08/05/what-is-the-difference-between-a-block/

+5
Sep 26 '14 at 8:29
source share

lambda is extremely useful in named_scope , so you can pass named_scopes parameters.

+3
Aug 05 '09 at 12:21
source share



All Articles