Heldop S. wrote:
@id = Animals.item
The Animals should be a class and the item should be a method.
but when I try to run this it gives: undefined method `item’ for
As Fred pointed out, you have defined an instance method, but you are
using it like a class method.
Class methods are methods invoked on a class and defined like this (not
the reference to “self” which here stands for the class itself):
These methods are independent of any particular Animal (known as
instances). They are designed to allow you to provide functionality
about the class itself, such as creating new instances, etc. You call
them like this:
Instance methods are methods invoked on a particular item of a class (an
instance of that class). These allow you to manipulate or investigate
specific instances of your class and are defined like this:
You call this on a particular Animal. You might get this from another
method, or create a new one yourself:
my_animal = Animals.new
Then you can call this instance method:
These concept of class and instance and the distinction between the
methods for each are at the core of Object Oriented Programming.