Ruby object print format

Consider the following:

irb(main):001:0> class A
irb(main):002:1> def initialize
irb(main):003:2> @string = "my string"
irb(main):004:2> end
irb(main):005:1> def to_s
irb(main):006:2> puts @string
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> a = A.new
=> #<A:0x2ea606c @string="my string">
irb(main):010:0> puts a
my string
#<A:0x2ea606c>
=> nil

      

When 'puts' outputs the string my, does it add the refference of the object as well?

Is there a way to get rid of this behavior? What I want to get is just a @string as defined in the to_s class method

Thank,

RM

+1


source to share


1 answer


You want your to_s method to just return @string, not puts @string

.



+4


source







All Articles