What is the -> (stab) operator in Ruby? [duplicate]

ghz 1years ago ⋅ 2773 views

Question

This question already has answers here :

[What do you call the -> operator in Ruby?](/questions/8476627/what-do-you- call-the-operator-in-ruby) (3 answers)

Closed 9 years ago.

In the following example:

default: -> { Time.now }

What's ->? I am familiar with => but first time I am seeing ->.


Answer

It's the Ruby 1.9 "stabby lambda" operator. For example, see this article from 2008.

Nutshell:

> foo2 = ->(arg) { arg * 2 }
> foo2.call "now"
 => nownow

Note the lack of space between -> and (arg), that's intentional.