I feel this is fundamental to my understanding of Ruby and object-oriented programming in general, so I’m asking this fairly simplistic question here at the risk of looking foolish. I’ve been toying around with irb. I’ve created my first ever class:
$ irb
ruby-1.9.2-p290 :001 > class Person
ruby-1.9.2-p290 :002?> attr_accessor :firstname, :lastname, :gender
ruby-1.9.2-p290 :003?> end
=> nil
ruby-1.9.2-p290 :004 > person_instance = Person.new
=> #<Person:0x007f9b7a9a0f70>
ruby-1.9.2-p290 :005 > person_instance.firstname = "Bob"
=> "Bob"
ruby-1.9.2-p290 :006 > person_instance.lastname = "Dylan"
=> "Dylan"
ruby-1.9.2-p290 :007 > person_instance.gender = "male"
=> "male"
So Person.new is my object, right? Or is my object the combination of class Person and the attributes I’ve defined for that class?
Your object is the result of running
Person.new, which you’ve captured inperson_instance.In ruby, attributes don’t actually exist until they are first written, so before
person_instance.firstname = "Bob", your instance has no attributes. After executing this statement it has a@firstnameattribute, but no others.