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

180
Vistas
Ruby: check if object has a method with particular signature, like respond_to?

Is there a way to check if particular method signature is present in Ruby?

For example I want to call

thing.make(env: @@__ENV__, apiKey: "myKey")

if I do this check

if thing.respond_to? 'make'

I can end up with the error

ArgumentError: unknown keyword: :apiKey

Is there a way to check that there is the particular make(env:,apiKey:) method and not just make with any arguments

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Simplest thing to do is try it and rescue the ArgumentError.

begin
  thing.make(env: @@__ENV__, apiKey: "myKey")
rescue ArgumentError => e
  ...guess not...
end

You can also introspect the parameters of the Method object. This returns an Array of Arrays like [[:key, :env], [:key, :apiKey]] . You're looking for :key if its optional, :keyreq if it's required.

params = thing.method(:make).parameters
p [:env,:apiKey].all? { |arg|
  params.include?([:key,arg]) || params.include?([:keyreq, arg])
}

If you have to do this as part of application code, you may want to reconsider your design.

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