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=>\"[email protected]\", :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?
CodePudding user response:
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.