Home > Software design >  How to print an object
How to print an object

Time:01-09

How do you change what is printed with puts when an object is referenced?

Consiser the following code:

class MyClass
    attr_accessor :var
    def initialize(var)
        @var = var
    end
    # ...
end
obj = MyClass.new("content")
puts obj # Prints #<MyClass:0x0000011fce07b4a0> but I want it to print "content"

I imagine that there is an operator that you can overload (or something similar), but I don't know what it's called so I have no clue what to search for to find the answer.

CodePudding user response:

Quote from the documentation of puts:

puts(*objects)nil

Writes the given objects to the stream, which must be open for writing; returns nil. Writes a newline after each that does not already end with a newline sequence. [...]

Treatment for each object:

  • String: writes the string.
  • Neither string nor array: writes object.to_s.
  • Array: writes each element of the array; arrays may be nested.

That means: The object you pass to puts is not a string, therefore, Ruby will call to_s on that object before outputting the string to IO. Because your object has no to_s method implemented, the default implementation from Object#to_s.

To return a customize output, just add your own to_s method to your class like this:

class MyClass
  attr_accessor :var

  def initialize(var)
    @var = var
  end

  def to_s
    var
  end
end

CodePudding user response:

class MyClass
  attr_accessor :var
  def initialize(var)
    @var = var
  end

  def to_s
    @var
  end
end

obj = MyClass.new("content")
puts obj # Prints "content"
  • Related