What is the operator & # 8594; (dash more) in Ruby / Rails

I simply typed the following line of code in a Rails application:

scope :for_uid, ->(external_id) { where(external_id: external_id) } 

What does operator -> mean? This is difficult for Google.

+7
ruby ruby-on-rails
source share
3 answers

This is syntactic sugar.

 ->(external_id) { where(external_id: external_id) } 

equally:

 lambda { |external_id| where(external_id: external_id) } 
+15
source share

new lambda notation . This syntax was introduced in ruby ​​1.9 and is used to define unnamed functions.

In your example, this is the area defined by an unnamed function.

+6
source share

The -> operator was introduced in Ruby 1.9 as a shorthand syntax for the old lambda function. It works almost identically to the lambda function, but allows you to specify parameters outside the block:

 lambda {|param| puts param } # becomes -> (param) { puts params } 
+5
source share

All Articles