ruby
35 lines · 7 steps
Currying lambdas and methods in Ruby
Ruby's Proc#curry turns a multi-argument callable into one that accepts its arguments one at a time.
Explained by
highlit
1# Curry a multi-argument lambda so it can be applied one argument at a time.
2add = ->(a, b, c) { a + b + c }
3
4# Proc#curry returns a curried version that collects arguments incrementally.
5curried_add = add.curry
6
7step1 = curried_add[1] # => #<Proc> still waiting for two more args
8step2 = step1[2] # => #<Proc> waiting for one more arg
9result = step2[3] # => 6, fully applied
10
11# You can also supply several arguments at once.
12curried_add[1][2, 3] # => 6
13curried_add[1, 2, 3] # => 6
14
15# Methods become curryable by first converting them to a Proc.
16class Discounter
17 def apply(rate, base_price)
18 base_price - (base_price * rate)
19 end
20end
21
22discounter = Discounter.new
23discount = discounter.method(:apply).to_proc.curry
24
25# Build specialized functions by partially applying the first argument.
26ten_percent_off = discount[0.10]
27half_off = discount[0.50]
28
29ten_percent_off[100] # => 90.0
30half_off[100] # => 50.0
31
32# curry(arity) lets you fix how many arguments to expect for variadic procs.
33join = ->(*parts) { parts.join('/') }
34path = join.curry(3)
35full = path['usr']['local']['bin'] # => "usr/local/bin"
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Currying lets you fix some arguments now and supply the rest later, building specialized functions from general ones.
- 2A curried proc returns either another proc (when under-applied) or the final result (when fully applied), so you can chain calls flexibly.
- 3Methods aren't procs, but method(:name).to_proc bridges the gap so any method can be curried too.
Related explainers
ruby
class ToastBroadcaster include ActionView::RecordIdentifier def self.broadcast_to(user, message:, type: :notice)
How Turbo Stream toasts broadcast in Rails
turbo-streams
service-object
real-time
Intermediate
6 steps
javascript
const ROLE_PERMISSIONS = { admin: ['users:read', 'users:write', 'billing:read', 'billing:write'], manager: ['users:read', 'billing:read'], member: ['users:read'],
Role-based permissions middleware in Express
authorization
middleware
rbac
Intermediate
9 steps
ruby
class TemplateInterpolator PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/ def initialize(strict: false)
Interpolating templates with dotted keys in Ruby
regex
string-interpolation
hash-traversal
Intermediate
6 steps
ruby
class KeyTransformer def self.camelize(data) new.camelize(data) end
Recursively camelizing nested Ruby data
recursion
data-transformation
pattern-matching
Intermediate
7 steps
ruby
module Paginatable extend ActiveSupport::Concern private
A reusable pagination concern in Rails
pagination
concerns
http-headers
Intermediate
8 steps
ruby
class Api::MessagesController < ApiController before_action :authenticate_api_key! rate_limit to: 100,
Layered API rate limiting in Rails
rate-limiting
api-authentication
throttling
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/currying-lambdas-and-methods-in-ruby-explained-ruby-315f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.