Skip to content

Instantly share code, notes, and snippets.

Created January 16, 2017 09:00
Show Gist options
  • Save anonymous/b14f91074ce8db48eede122a8ad55407 to your computer and use it in GitHub Desktop.
Save anonymous/b14f91074ce8db48eede122a8ad55407 to your computer and use it in GitHub Desktop.
class Person
def initialize(name,age)
@name = name
@age = age
end
end
person = {}
i = 0
while i < 5
puts "Enter your name:"
name = gets.chomp
puts "Enter your age:"
age = gets.chomp.to_i
i += 1
person[name.to_sym] = Person.new(name,age)
end
person.each do |key,value|
puts "Name is #{key}"
puts "Age is #{value}"
end
--------------------------------------
output:
Name is ohoh
Age is #<Person:0x00000002fd34f8>
Name is hehe
Age is #<Person:0x00000002fd32a0>
Name is heeh
Age is #<Person:0x00000002fd3048>
Name is hoho
Age is #<Person:0x00000002fd2e18>
Name is yeyo
Age is #<Person:0x00000002fd2bc0>
@baweaver
Copy link

class Person
  attr_accessor :name, :age

  def initialize(name, age)
    @name = name
    @age  = age
  end
end

people = 5.times.each_with_object({}) { |i, people|
  puts "Enter your name:"
  name = gets.chomp
  puts "Enter your age:"
  age = gets.chomp.to_i
  people[name.to_sym] = Person.new(name,age)
}

people.each do |key,value|
  puts "Name is #{key}"
  puts "Age is #{value}"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment