Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda