0%
Reading Settings
Font Size
18px
Line Height
1.5
Letter Spacing
0.01em
Font Family
Table of contents
    blog cover

    Ruby Proc, Lambda, and Blocks

    Ruby on Rails
    Ruby on Rails
    published 2024-07-10 18:07:07 +0700 · 3 mins read
    In Ruby, there are three powerful constructs that allow for flexible and reusable code: Procs, Lambdas, and Blocks. These constructs play a significant role in making Ruby a highly expressive and dynamic language. In this blog post, we will explore each of them and understand their similarities, differences, and use cases.

    1. Blocks

    Blocks are chunks of code enclosed within either curly braces {} or do...end. They are not objects and cannot be stored in variables like Procs and Lambdas. Blocks are primarily used to pass behavior to methods and are commonly seen in iterators and method invocations.
    Iterating over an Array
    // language: ruby
    [1, 2, 3].each do |num|
      puts num * 2
    end
    # Output:
    # 2
    # 4
    # 6

    Custom Method with a Block
    // language: ruby
    def greet(name)
      puts "Hello, #{name}!"
      yield
      puts "Goodbye, #{name}!"
    end
    
    greet("John") do
      puts "Have a great day!"
    end
    # Output:
    # Hello, John!
    # Have a great day!
    # Goodbye, John!
    Blocks are highly flexible and often used for one-time, ad-hoc behavior that is specific to a particular method invocation.

    2. Procs

    Procs, short for procedures, are objects that encapsulate blocks of code and allow them to be stored, passed around, and executed at a later time. They are created using the Proc.new or the proc method.
    Define and call the Proc
    // language: ruby
    my_proc = Proc.new { |x| puts x * 2 }
    my_proc.call(3)   # Output: 6

    Custom Method with a Proc Parameter
    // language: ruby
    def perform_operation(a, b, operation)
      result = operation.call(a, b)
      puts "The result is: #{result}"
    end
    
    add = Proc.new { |x, y| x + y }
    perform_operation(5, 3, add)  # Output: The result is: 8

    Passing Proc as a parameter
    // language: ruby
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    even = Proc.new { |num| num.even? }
    even_numbers = numbers.select(&even)   # => [2, 4, 6, 8, 10]

    One significant advantage of Procs is their ability to capture variables from their surrounding context, even after the context is gone. This property is known as closures and allows Procs to maintain access to variables that were in scope when the Proc was defined.

    3. Lambdas

    Lambdas are similar to Procs in that they encapsulate blocks of code and can be stored and executed later. However, they have some subtle differences in behavior.
    Lambdas are created using the lambda keyword or the -> syntax. They enforce strict argument handling and return behavior.

    You might notice, when you create a lambda in Ruby, what you get is actually a Proc object (with a special lambda flag):
    // language: ruby
    3.2.2 :001 > my_lambda = lambda { |x| puts x * 2 }
     => #<Proc:0x000000011d8ec6c8 (irb):1 (lambda)>
    3.2.2 :002 > my_lambda.call(3)
    6
     => nil
    3.2.2 :003 > my_lambda = ->(x) { puts x * 2 }
     => #<Proc:0x0000000102457bc8 (irb):3 (lambda)>
    3.2.2 :004 > my_lambda.call(3)
    6
     => nil

    If we try to call the Lambda with the wrong number of arguments, an ArgumentError will be raised.
    // language: ruby
    my_lambda.call(3, 4)
    # ArgumentError (wrong number of arguments (given 2, expected 1))

    Unlike Procs, Lambdas have a stricter interpretation of return statements. When a Lambda encounters a return statement, it only returns from the Lambda itself, whereas a Proc would return from the surrounding context as well.
    // language: ruby
    def proc_return
      Proc.new { return "proc return" }.call
      return "proc_return method finished"
    end
    proc_return
    # => "proc return"
    
    def lambda_return
      lambda { return "lambda return" }.call
      return "lambda_return method finished"
    end
    lambda_return
    # => "lambda_return method finished"

    4. Conclusion

    In summary, Ruby offers three powerful constructs: Blocks, Procs, and Lambdas for handling reusable chunks of code.
     
    Blocks are temporary and mainly used to pass behavior to methods. They aren’t objects and can’t be stored in variables.
    Procs and Lambdas, on the other hand, are both objects that can be assigned, passed, and reused. Procs are more flexible about arguments and return behavior, and Lambdas enforce stricter checks and localized returns.

    Related blogs