Search code examples
rubyself

What's the difference between #<MyClass:0x10f6a82d0> and MyClass?


class MyClass
  def fun
    puts self
  end

  def self.fun2
    puts self
  end
end

mine = MyClass.new
mine.fun
MyClass.fun2

The above code should print self twice, in which case they are both MyClass. However, the actual output is

#<MyClass:0x10f6a82d0>
MyClass

which are in two difference forms. I wonder if they are actually representing different things.


Solution

  • The first result is the string representation of an instance of MyClass, whereas the second result is the string representation of the class MyClass.

    If you had made another mine2 = MyClass.new and printed it, you'd get a different hex identifier at the end to distinguish it from your other instance.