Meaning of -> shorthand in Ruby
friendly-id, ruby, ruby-on-rails
Solution
This denotes the `lambda`. With this you are latching an anonymous function which takes a parameter config and computes a block using that variable.
The above expression can also be defined as:
@defaults ||= lambda {|config| config.use :reserved}
`Proc` is similar to `lambda` in Ruby, apart from few differences of return and break pattern. Proc can be called as a block saved as an object, while lambda is a method saved as an object. They find their roots in functional programming.
In short, a lambda is a named procedure, which can be saved as an object and can be called later.
inc = ->x{ x + 1 }
inc.call(3)
#=> 4
One common and interesting example of `lambda` is Rails Scope, where a method is simply assigned in name scope as lambda and can be later used as an action while ActiveRecord querying.
Problem
I was browsing Friendly_id gem code base and I found line with following assignment: ``` @defaults ||= ->(config) {config.use :reserved} ``` My questions are: - How do I interpret this line of code? - What does exactly `->` do and what does it mean? - Is there any articles about it, how to use it?(Official Ruby documentation would be nice, I haven't found it) Thank you for your help