passing hash as arguments

J

Jason Lillywhite

Is this the best way (though contrived) to use a hash as an argument in
ruby?

def state_name_age(feeling, info{})
"#{:name} is #{feeling} #{:age} #{:units} old"
end

my_info = {}
my_info = {:name => "Kate", :units => "years", :age => 24}

irb(main):018:0> state_name_age('happily', my_info)
=> "Kate is happily 24 years old"

then...

irb(main):068:0> state_name_age 'happily'
=> " is happily old!"

This seems to be the way Rails has things set up to pass optional
arguments... Is that correct?
 
T

Tom Link

def state_name_age(feeling, info{})
=A0 "#{:name} is #{feeling} #{:age} #{:units} old"
end

Try:

def state_name_age(feeling, info=3D{})
"#{info[:name]} is #{info[feeling]} #{info[:age]} #{info[:units]}
old"
end

irb(main):018:0> state_name_age('happily', :name =3D> "Kate", :units =3D>
"years", :age =3D> 24)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top