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

312
Visualizações
Passing a hash as arguments in Ruby

I have the following code:

user = User.from_google(from_google_params)

  def from_google_params
    @from_google_params ||= {
      uid: auth.uid,
      email: auth.info.email,
      full_name: auth.info.name,
      avatar_url: auth.info.image
    }
  end

...and here is the from_google method the hash is being passed to.

def self.from_google(email:, full_name:, uid:, avatar_url:)

When I run this I am getting an argument error ArgumentError (wrong number of arguments (given 1, expected 0; required keywords: email, full_name, uid, avatar_url)):

Here is what I see in the console:

>> from_google_params.inspect
=> "{:uid=>\"100373362533979950619\", :email=>\"marklocklear@extension.org\", :full_name=>\"Mark Locklear\", :avatar_url=>\"https://lh3.googleusercontent.com/a-/AOh14Gh1leKarrW8SCvxxwvaAcDkAISXYQ38k4xPQuC1Rg=s96-c\"}"
>> from_google_params.class
=> Hash

How can I make this work?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use the double splat operator (**) to convert the hash into keyword arguments.

user = User.from_google(**from_google_params)

The separation of keywords and positional arguments was a big change in Ruby 3. Previous versions of Ruby would coerce the last positional argument into keywords if its a hash - this behavior was depreciated in Ruby 2.7 and removed completely in 3.0.

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