Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -> mean in Ruby [duplicate]

I've seen that in spree commerce.

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

So what'll do that symbol?

like image 668
shajin Avatar asked Dec 26 '22 04:12

shajin


1 Answers

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 accept arguments.

l1 = lambda(name) { puts "I'm a #{name}" }
l2 = ->(name) { puts "I'm a #{name}" } 
like image 149
Simone Carletti Avatar answered Jan 07 '23 23:01

Simone Carletti