Ruby Lambda

christian.dinh's avatar
Published Jul 30, 2021Updated Sep 9, 2021
Contribute to Docs

In Ruby, lambdas are anonymous function code blocks that can take zero or more arguments. They can then be stored or passed in other values and called primarily with the #call method.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.
    • Beginner Friendly.
      9 hours

Syntax

If zero arguments are used, use either -> or lambda:

myLambda = lambda { puts "Hello, World!" }
puts myLambda.call # Output: Hello, World!

If 1 or more arguments are used, use ->:

myLambda = -> (v) { v * 2 }
puts myLambda.call(2) # Output: 4

All contributors

Contribute to Docs

Learn Ruby on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn to program in Ruby, a flexible and beginner-friendly language used to create sites like Codecademy.
    • Beginner Friendly.
      9 hours