Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
Identity method factory in Ruby

Is there a shortcut in Ruby to receive a lambda which simply returns its argument?

I'm trying to have the following function:

def some_method(x, y, decorator = ->(x) { x })
  …
end

Rewritten to look somehow like

def some_method(x, y, decorator = method(:itself))
  …
end

Maybe there's a way to change the receiver with Object#itself?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have probably seen code like this plenty of times in ruby:

array.map(&:upcase)

Well, what does &:upcase actually mean? It's actually calling Symbol#to_proc.

Therefore, you could choose to define the method like this:

def some_method(x, y, decorator = :itself.to_proc)
  # ...
end
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!