I don't understand the output of my first example: print returns nil, so
print 'dog' should return nil, which means to_s returns nil, and so
print d should be equivalent to print nil.
In IO#write, if #to_s returns nil, rb_any_to_s() gets called, which returns "#<#{self.class}:0x#{object_id.to_s 16}>", which gets printed instead of your broken #to_s.
···
On Oct 10, 2007, at 19:58 , 7stud -- wrote:
Eric Hodel wrote:
On Oct 10, 2007, at 15:31 , Martin DeMello wrote:
=> foo
it calls #to_s.
class Dog
def to_s
print "dog"
end
end
d = Dog.new
print d
puts
puts d
--output:--
dog#<Dog:0x25634>
--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars
If to_s returns something other than a string, Ruby shakes a naughty
finger at you and ignores you, and falls back on the result of
Kernel#inspect instead.
irb(main):001:0> class Foo; def to_s; 42; end; end
=> nil
irb(main):002:0> puts Foo.new
#<Foo:0x359224>
=> nil
irb(main):003:0> class Foo; def to_s; "42"; end; end
=> nil
irb(main):004:0> puts Foo.new
42
···
On Oct 10, 9:25 pm, 7stud -- <dol...@excite.com> wrote:
I don't understand the output of my first example: print returns nil, so
print 'dog' should return nil, which means to_s returns nil, and so
print d should be equivalent to print nil.
I'm guessing puts/print treat nil as a special case, but I may be wrong.
That's quite counterintuitive, though. If true, the question becomes
"why?" - I can't see any useful purpose it'd serve, save maybe
debugging.
martin
pickaxe2, p. 131:
"With a couple of exceptions, every object you pass to puts and print is
converted to a string by calling that object's to_s method. If for some
reason the to_s method doesn't return a valid string, a string is
created containing the object's class name and ID....
The exceptions are simple, too. The nil object will print as the string
'nil', and an array passed to puts will be written as if each of its
elements in turn were passed separately to puts."
Eric Hodel wrote:
···
On 10/10/07, Konrad Meyer <konrad@tylerc.org> wrote:
On Oct 10, 2007, at 19:58 , 7stud -- wrote:
end
dog#<Dog:0x25634>
In IO#write, if #to_s returns nil, rb_any_to_s() gets called, which
returns "#<#{self.class}:0x#{object_id.to_s 16}>", which gets printed
instead of your broken #to_s.
On Thu, 2007-10-11 at 15:26 +0900, Martin DeMello wrote:
That's quite counterintuitive, though. If true, the question becomes
"why?" - I can't see any useful purpose it'd serve, save maybe
debugging.
Debugging is right. Otherwise, you print nothing (or "" -- who knows?).
It's probably better to see 'nil' and have to toss up between print nil
and print "nil" (or print ["nil"], or whatever!), rather than never
seeing a thing and constantly guessing if you're perhaps seeing print
nil, "", [""], etc....
In IO#write, if #to_s returns nil, rb_any_to_s() gets called, which
returns "#<#{self.class}:0x#{object_id.to_s 16}>", which gets printed
instead of your broken #to_s.
How do you get from puts/print to IO.write?
--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars