I'm a newbie to Ruby and I don't understand why do I have the result that I have.
- I have a
Person
class in which I have the instance variables,@name
,@age
and@weight
. In this class I override the to_s method to print the@name
,@age
and@weight
of the object. I have a House class in which I have an array of objects of type
Person
(@persons
), and also I have a method calledperson
that returns an object (in the position that is specified with the parameter number) in the array@persons
, so for example,any_house_object.person(0)
will return the first object of the array of objects of typePerson
that is on theany_house_object
object.class Person def initialize(name, age, weight) @name = name @age = age @weight = weight end def to_s puts "Name: #{@name}" puts "Age: #{@age}" puts "Weight: #{@weight}" end end class House def initialize @persons = [] end def add_person(name, age, weight) @persons << Person.new(name, age, weight) end def person(number) @persons[number] end end my_house = House.new my_house.add_person("Patrick", "18", "65") my_house.add_person("John", "18", "65") puts my_house.person(0)
This produce
Name: Patrick
Age: 18
Wight: 65
#<Person:0x5e2318>
Finally, my problem is that I don't understand why at the end, when I do puts my_house.person(0)
also appears #<Person:0x5e2318>
at the end.