L
Leslie Viljoen
I read somewhere that "puts" calls "to_s" on an object to get a string
representation and then displays that. This seems to be correct in my
experience.
My question is: how does puts bypass "private" restrictions to get the
value of to_s?
See here:
irb(main):005:0> class RiseAndShine
irb(main):006:1> private
irb(main):007:1> def to_s
irb(main):008:2> "rising and shining"
irb(main):009:2> end
irb(main):010:1> end
=> nil
irb(main):011:0> rs = RiseAndShine.new
=> rising and shining
irb(main):012:0> rs.to_s
NoMethodError: private method `to_s' called for rising and shining:RiseAndShine
from (irb):12
irb(main):013:0> puts rs
rising and shining
=> nil
irb(main):014:0>
representation and then displays that. This seems to be correct in my
experience.
My question is: how does puts bypass "private" restrictions to get the
value of to_s?
See here:
irb(main):005:0> class RiseAndShine
irb(main):006:1> private
irb(main):007:1> def to_s
irb(main):008:2> "rising and shining"
irb(main):009:2> end
irb(main):010:1> end
=> nil
irb(main):011:0> rs = RiseAndShine.new
=> rising and shining
irb(main):012:0> rs.to_s
NoMethodError: private method `to_s' called for rising and shining:RiseAndShine
from (irb):12
irb(main):013:0> puts rs
rising and shining
=> nil
irb(main):014:0>