I'm new to learning Ruby and am having a little trouble with nested structures.
What I'm trying to do is return :last name ("Doe") if the value of :first is "John":
people = {
:family => [
{:first => "John", :last => "Doe"},
{:first => "Jane", :last => "Smith"}
]
}
people[:family].each {|index| return index[:last] if index[:first] == "John"}
However, the console is giving me this error:
test.rb:8:in `block in <main>': unexpected return (LocalJumpError)
from test.rb:8:in `each'
from test.rb:8:in `<main>'
When I test this in the console and replace 'return' with 'puts', it gives me back "Doe" but for some reason the 'return' seems to be causing the 'unexpected return (LocalJumpError)'. How can I successfully return this value without running into this error?
Thanks - any and all help is greatly appreciated!