What does & # 8594; means in Ruby

I saw this in spree trading.

go_to_state :confirm, if: ->(order) { order.confirmation_required? } 

So what will this symbol do?

+4
source share
2 answers

This is a lambda literal. Check out this example:

  > plus_one = ->(x){x+1} => #<Proc: 0x9fbaa00@ (irb):3 (lambda)> > plus_one.call(3) => 4 

Lambda literal is a constructor for Proc . A Proc is a way to have a code block assigned to a variable. After that, you can call your code block again with different arguments as many times as you like.

Here's how you can pass a β€œfunction” as a parameter in a ruby. In many languages, you can pass a link to a function. In ruby ​​you can pass a proc object.

+4
source

In Ruby 1.9, you can use the stab -> operator to create a lambda.

 l1 = lambda { puts "I'm a lambda" } l2 = -> { puts "I'm a lambda" } 

The operator also accepts arguments.

 l1 = lambda(name) { puts "I'm a #{name}" } l2 = ->(name) { puts "I'm a #{name}" } 
+6
source

All Articles