April 14, 2025
If you’ve been working with Ruby for a while, you’ve likely encountered blocks , procs , and lambdas. But beneath these familiar constructs lies a concept that gives them real power: closures.
Need Help with Ruby or Rails?
I help businesses build, optimize, and maintain Ruby and Ruby on Rails applications — with a strong focus on clean code, performance, and real-world problem solving.
If you’re looking for a developer with experience, empathy, and a love for elegant solutions, let’s talk.
Get in touch here — I’d love to hear about your project.
So, What Is a Closure?
A closure is a function or block of code that captures and remembers the environment in which it was defined — even when it’s executed outside of that scope.
In Ruby, closures allow us to write cleaner, more flexible, and more powerful code.
Let’s take a simple example:
def multiplier(factor)
->(n) { n * factor }
end
times_two = multiplier(2)
times_five = multiplier(5)
puts times_two.call(10) # => 20
puts times_five.call(10) # => 50
Even though factor is a local variable inside the multiplier method, the lambda returned by it retains access to factor — that’s the closure in action.
Why It Matters in Real Projects
Closures are essential for:
Encapsulating behavior without polluting the global or class namespace
Creating flexible APIs , like Rails scopes or middleware stacks
Delaying execution — think of callbacks or background jobs
Functional programming patterns , like currying or chaining
You’ve likely seen it in Rails:
class User < ApplicationRecord
scope :active, -> { where(active: true) }
end
That lambda is a closure — capturing no external variables in this case, but still a complete, standalone chunk of logic.
Bonus: Closures & State
Ruby closures can also maintain internal state in clever ways:
def counter
count = 0
-> { count += 1 }
end
my_counter = counter
puts my_counter.call # => 1
puts my_counter.call # => 2
That count variable is private to the closure and persists between calls.
Final Thoughts
Closures are one of Ruby’s most elegant features — blending the best of functional and object-oriented programming.
If you’re building tools, APIs, or just want to reduce duplication in your codebase, understanding closures isn’t optional — it’s a superpower.
Have you used closures in an interesting way in your Ruby projects? Let’s connect and share ideas!
Top comments (0)