Hash value into the string

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?

Do you want to do this?

def describe(user_info)
“My name is #{user_info[:name]} and I’m #{user_info[:age]} years old.”
end

user_info = {:name=>“Greg Lief”, :age => 42}
puts describe(user_info)

This outputs

My name is Greg Lief and I’m 42 years old.

See Hash#[]
http://ruby-doc.org/core-2.3.0/Hash.html#method-i-5B-5D

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs