In Ruby, is there a difference between `self.i` and `@i`?

There is, of course, a difference betwenn self.i and @i:

  • @i will return the value assigned to the variable @i.
  • Whereas self.i will return the value which was returned by a method named i.

Your Animal class has no i method defined, therefore the error message is correct.

When you want self.i to return the value of the @i variable then you need to create a i method like this

def i

or simply use the attr_reader macro like this:

class Animal
  attr_reader :i