Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby lambda literal syntax

Tags:

ruby

lambda

I have a doubt on the lambda literal syntax corresponding to that (more classic) lambda syntax:

lambda { |foo| }

Is the following correct?

->(foo) {}

Thank you.

like image 964
Mich Avatar asked Apr 30 '14 10:04

Mich


People also ask

How do you define 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. proc_demo = Proc. new { return "Only I print!" }

What is the difference between a proc and a lambda?

There are only two main differences. First, a lambda checks the number of arguments passed to it, while a proc does not. This means that a lambda will throw an error if you pass it the wrong number of arguments, whereas a proc will ignore unexpected arguments and assign nil to any that are missing.

What are procs in Ruby?

A Proc object is an encapsulation of a block of code, which can be stored in a local variable, passed to a method or another Proc, and can be called. Proc is an essential concept in Ruby and a core of its functional programming features.


1 Answers

That is correct. More information about that in the official documentation: http://www.ruby-doc.org/core-2.1.1/doc/syntax/literals_rdoc.html#label-Procs

like image 160
Martin Avatar answered Sep 22 '22 11:09

Martin