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

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

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 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