Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda