Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

In the following example:

default: -> { Time.now } 

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

like image 481
0xSina Avatar asked Feb 18 '12 09:02

0xSina


People also ask

What does -> mean in Ruby?

This answer is not useful. Show activity on this post. In Ruby Programming Language ("Methods, Procs, Lambdas, and Closures"), a lambda defined using -> is called lambda literal. succ = ->(x){ x+1 } succ.call(2) The code is equivalent to the following one.

What is Proc and lambda in Ruby?

In Ruby, a lambda is an object similar to a proc. Unlike a proc, a lambda requires a specific number of arguments passed to it, and it return s to its calling method rather than returning immediately. def proc_demo_method. proc_demo = Proc. new { return "Only I print!" }


Video Answer


1 Answers

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.

like image 196
Dave Newton Avatar answered Sep 23 '22 02:09

Dave Newton