Back to home

Simple Example
Ruby Lambda Functions main image

Ruby Lambda Functions

The basic gist of declaration and usage is to use the lambda keyword. Note that Lambda functions have subtle differences to Proc functions, although they both derive from the Proc object.

The Lambda function enforces the number of arguments passed.

In addition, Lambda and Proc functions treat the return differently. Lambda will return from the scope of that closure function, whereas Proc will return from the parent scope.

Simple Example

1 2 3 4 5 6 7 8 9 10 11 12 # lambda.rb class Lambda def self.hello helloWorld = lambda { return "Hello, World!" } res = helloWorld.call end def self.sum(a, b) sumLambda = lambda {|a,b| a + b } res = sumLambda.call(a, b) end end

Test file

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 # lambda_test.rb begin gem 'minitest', '>= 5.0.0' require 'minitest/autorun' require_relative 'lambda' rescue Gem::LoadError => e puts "\nMissing Dependency:\n#{e.backtrace.first} #{e.message}" puts 'Minitest 5.0 gem must be installed for the Ruby track.' rescue LoadError => e puts "\nError:\n#{e.backtrace.first} #{e.message}" puts DATA.read exit 1 end # Common test data version: 1.1.0 be3ae66 class LambdaTest < Minitest::Test def test_hello_world_lambda # skip assert_equal "Hello, World!", Lambda.hello end def test_sum_lambda # skip assert_equal 3, Lambda.sum(1,2) end end __END__

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.