OneZero
OneZero

Reputation: 11904

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.

Upvotes: 1

Views: 40

Answers (2)

Dan Wich
Dan Wich

Reputation: 4943

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.

Upvotes: 1

Richard Brown
Richard Brown

Reputation: 11436

#<MyClass:0x10f6a82d0> represents an instance of the class MyClass. MyClassf.fun2 returns the class itself.

Upvotes: 1

Related Questions