[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

what does print call internally?

Martin DeMello

10/10/2007 10:31:00 PM

irb(main):001:0> puts nil
nil
=> nil
irb(main):002:0> print nil
nil=> nil
irb(main):003:0> class NilClass
irb(main):004:1> def inspect
irb(main):005:2> "foo"
irb(main):006:2> end
irb(main):007:1> end
=> foo
irb(main):008:0> puts nil
nil
=> foo
irb(main):009:0> nil.to_s
=> ""
irb(main):010:0> $_
=> foo

$ ruby -v
ruby 1.8.5 (2006-12-25 patchlevel 12) [x86_64-linux]

martin

13 Answers

Eric Hodel

10/11/2007 1:48:00 AM

0

On Oct 10, 2007, at 15:31 , Martin DeMello wrote:

> irb(main):001:0> puts nil
> nil
> => nil
> irb(main):002:0> print nil
> nil=> nil
> irb(main):003:0> class NilClass
> irb(main):004:1> def inspect
> irb(main):005:2> "foo"
> irb(main):006:2> end
> irb(main):007:1> end
> => foo

it calls #to_s.

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars



Joel VanderWerf

10/11/2007 2:52:00 AM

0

Eric Hodel wrote:
> On Oct 10, 2007, at 15:31 , Martin DeMello wrote:
>
>> irb(main):001:0> puts nil
>> nil
>> => nil
>> irb(main):002:0> print nil
>> nil=> nil
>> irb(main):003:0> class NilClass
>> irb(main):004:1> def inspect
>> irb(main):005:2> "foo"
>> irb(main):006:2> end
>> irb(main):007:1> end
>> => foo
>
> it calls #to_s.

Hm. What's going on here then:

$ ruby -e 'p nil.to_s'
""
$ ruby -e 'print nil'
nil

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

7stud 7stud

10/11/2007 2:59:00 AM

0

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>
dog#<Dog:0x25634>
--
Posted via http://www.ruby-....

7stud 7stud

10/11/2007 3:25:00 AM

0

7stud -- wrote:
>
> class Dog
> def to_s
> print "dog"
> end
> end
>
> d = Dog.new
>
> print d
> puts
>
> puts d
>
> --output:--
> dog#<Dog:0x25634>
> dog#<Dog:0x25634>
>

I guess that should be:

class Dog
def to_s
"dog"
end
end

d = Dog.new

print d
puts

puts d

--output:--
dog
dog


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.
--
Posted via http://www.ruby-....

Konrad Meyer

10/11/2007 3:26:00 AM

0

Quoth Joel VanderWerf:
> Eric Hodel wrote:
> > On Oct 10, 2007, at 15:31 , Martin DeMello wrote:
> >
> >> irb(main):001:0> puts nil
> >> nil
> >> => nil
> >> irb(main):002:0> print nil
> >> nil=> nil
> >> irb(main):003:0> class NilClass
> >> irb(main):004:1> def inspect
> >> irb(main):005:2> "foo"
> >> irb(main):006:2> end
> >> irb(main):007:1> end
> >> => foo
> >
> > it calls #to_s.
>
> Hm. What's going on here then:
>
> $ ruby -e 'p nil.to_s'
> ""
> $ ruby -e 'print nil'
> nil
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Irb calls NilClass#inspect, as the OP demonstrated.

irb(main):001:0> class NilClass; def inspect() "foo" end; end # => foo
irb(main):002:0> nil # => foo
irb(main):003:0> puts nil # => foo
nil

I'm guessing puts/print treat nil as a special case, but I may be wrong.

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Phrogz

10/11/2007 3:34:00 AM

0

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.

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

Martin DeMello

10/11/2007 6:24:00 AM

0

On 10/10/07, Eric Hodel <drbrain@segment7.net> wrote:
> On Oct 10, 2007, at 15:31 , Martin DeMello wrote:
>
> > irb(main):001:0> puts nil
> > nil
> > => nil
> > irb(main):002:0> print nil
> > nil=> nil
> > irb(main):003:0> class NilClass
> > irb(main):004:1> def inspect
> > irb(main):005:2> "foo"
> > irb(main):006:2> end
> > irb(main):007:1> end
> > => foo
>
> it calls #to_s.

How does print nil print "nil" then?

martin

Martin DeMello

10/11/2007 6:26:00 AM

0

On 10/10/07, Konrad Meyer <konrad@tylerc.org> wrote:
>
> 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

Eric Hodel

10/11/2007 7:12:00 AM

0


On Oct 10, 2007, at 19:52 , Joel VanderWerf wrote:

> Eric Hodel wrote:
>> On Oct 10, 2007, at 15:31 , Martin DeMello wrote:
>>> irb(main):001:0> puts nil
>>> nil
>>> => nil
>>> irb(main):002:0> print nil
>>> nil=> nil
>>> irb(main):003:0> class NilClass
>>> irb(main):004:1> def inspect
>>> irb(main):005:2> "foo"
>>> irb(main):006:2> end
>>> irb(main):007:1> end
>>> => foo
>> it calls #to_s.
>
> Hm. What's going on here then:
>
> $ ruby -e 'p nil.to_s'
> ""
> $ ruby -e 'print nil'
> nil

switch (TYPE(argv[i])) {
case T_NIL:
rb_io_write(out, rb_str_new2("nil"));
break;
default:
rb_io_write(out, argv[i]);
break;
}

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars



Eric Hodel

10/11/2007 7:18:00 AM

0

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>
> 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.

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars