Rafa_F
February 13, 2016, 6:06pm
1
I am doing exercise in ruby monk and i got this task
def describe(user_info)
p user_info
“My name is #{user_info[0]} and I’m #{user_info[1]} years old.”
end
user_info output would be :
{“name”=>“Greg Lief”, “age”=>42}
how do i pass hash values into the string?
def describe(user_info)
user_info.each do |num,value|
“My name is #{user_info[0]} and I’m #{user_info[1]} years old.”
end
end
I tried something like this but i am not sure there to go from here.
Could someone explain how to solve this?
modus
February 14, 2016, 2:01pm
3
Dont know why but your solution still ends up showing error…
def describe(user_info)
“My name is #{user_info[(“name”)]} and I’m #{user_info[(“age”)]} years
old.”
end
found this solution and it passes. Maybe it is just the way the exercise
works.
Thanks
modus
February 15, 2016, 12:54am
4
In ruby, there are Strings (“name”), and there are Symbols (:name).
In the example I posted I used Symbols (because those feel more
idiomatic). If your hash contains Strings as keys, you need to access
its elements with Strings.
To illustrate the difference, try this:
h = {:name => 3, “name” => 6}
puts h[:name]
puts h[“name”]