The hash can't be used in defs

Here is my code:

info = Hash.new

def hem

print “Name?”
name = gets.chomp.downcase.capitalize

print “Surname?”
surname = gets.chomp.upcase

print “Birthday (DD.MM.YYYY)?”
birthday = gets.chomp

full_name = name + " " + surname

info[full_name] = birthday

hem

end

I can’t use it (try it on Codecademy Labs) because “hem” (random name)
can’t access the “info” hash. Would it change if I used the hash literal
notation (info = {})?
Please help me.

Oh wait… Could it help if I said:

def hem(info)

print “Name?”
name = gets.chomp.downcase.capitalize

print “Surname?”
surname = gets.chomp.upcase

print “Birthday (DD.MM.YYYY)?”
birthday = gets.chomp

full_name = name + " " + surname

info[full_name] = birthday

hem({})

end

hem({})

Hem uses a new hash but maybe the hash will start all over

again…

Three possibilities:

  • Pass ‘info’ as a parameter to ‘hem’.

  • Make ‘info’ a global variable.

  • Wrap everything into a class and make ‘info’ a class member

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