what does print call internally?

Discussion in 'Ruby' started by Martin DeMello, Oct 10, 2007.

  1. 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
    Martin DeMello, Oct 10, 2007
    #1
    1. Advertising

  2. Martin DeMello

    Eric Hodel Guest

    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
    Eric Hodel, Oct 11, 2007
    #2
    1. Advertising

  3. 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
    Joel VanderWerf, Oct 11, 2007
    #3
  4. Martin DeMello

    7stud -- Guest

    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-forum.com/.
    7stud --, Oct 11, 2007
    #4
  5. Martin DeMello

    7stud -- Guest

    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-forum.com/.
    7stud --, Oct 11, 2007
    #5
  6. Martin DeMello

    Konrad Meyer Guest

    --nextPart1742221.jO4t3LgaTi
    Content-Type: text/plain;
    charset="iso-8859-1"
    Content-Transfer-Encoding: quoted-printable
    Content-Disposition: inline

    Quoth Joel VanderWerf:
    > Eric Hodel wrote:
    > > On Oct 10, 2007, at 15:31 , Martin DeMello wrote:
    > >=20
    > >> irb(main):001:0> puts nil
    > >> nil
    > >> =3D> nil
    > >> irb(main):002:0> print nil
    > >> nil=3D> 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
    > >> =3D> foo

    > >=20
    > > it calls #to_s.

    >=20
    > Hm. What's going on here then:
    >=20
    > $ ruby -e 'p nil.to_s'
    > ""
    > $ ruby -e 'print nil'
    > nil
    >=20
    > --=20
    > 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 # =3D> foo
    irb(main):002:0> nil # =3D> foo
    irb(main):003:0> puts nil # =3D> foo
    nil

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

    =2D-=20
    Konrad Meyer <> http://konrad.sobertillnoon.com/

    --nextPart1742221.jO4t3LgaTi
    Content-Type: application/pgp-signature; name=signature.asc
    Content-Description: This is a digitally signed message part.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (GNU/Linux)

    iD8DBQBHDZekCHB0oCiR2cwRAoIiAJ4zDtSb9Vkz1s0O4fASSQ7cDQnyHACgznWs
    oVfN+75m30PrtFH2tlbGIHo=
    =5nKb
    -----END PGP SIGNATURE-----

    --nextPart1742221.jO4t3LgaTi--
    Konrad Meyer, Oct 11, 2007
    #6
  7. Martin DeMello

    Phrogz Guest

    On Oct 10, 9:25 pm, 7stud -- <> 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
    Phrogz, Oct 11, 2007
    #7
  8. On 10/10/07, 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.


    How does print nil print "nil" then?

    martin
    Martin DeMello, Oct 11, 2007
    #8
  9. On 10/10/07, Konrad Meyer <> 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
    Martin DeMello, Oct 11, 2007
    #9
  10. Martin DeMello

    Eric Hodel Guest

    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)) {
    case T_NIL:
    rb_io_write(out, rb_str_new2("nil"));
    break;
    default:
    rb_io_write(out, argv);
    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, Oct 11, 2007
    #10
  11. Martin DeMello

    Eric Hodel Guest

    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
    Eric Hodel, Oct 11, 2007
    #11
  12. Martin DeMello

    7stud -- Guest

    Martin DeMello wrote:
    > On 10/10/07, Konrad Meyer <> 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


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


    How do you get from puts/print to IO.write?
    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Oct 11, 2007
    #12
  13. Martin DeMello

    Eric Hodel Guest

    On Oct 11, 2007, at 05:03 , 7stud -- wrote:
    > Eric Hodel 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.

    >
    > How do you get from puts/print to IO.write?


    Follow the calls in rb_f_print() (Kernel#print).

    --
    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, Oct 11, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Maz
    Replies:
    2
    Views:
    457
    Jon Maz
    Sep 30, 2004
  2. responsible
    Replies:
    3
    Views:
    343
    Juha Nieminen
    Jun 3, 2007
  3. mathog
    Replies:
    11
    Views:
    804
  4. Yohan N. Leder
    Replies:
    19
    Views:
    232
    Yohan N. Leder
    Jul 2, 2006
  5. Helmut Tessarek

    how does $#array work internally?

    Helmut Tessarek, Nov 8, 2012, in forum: Perl Misc
    Replies:
    11
    Views:
    340
    Keith Thompson
    Nov 11, 2012
Loading...

Share This Page