Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > what does print call internally?

Reply
Thread Tools

what does print call internally?

 
 
Martin DeMello
Guest
Posts: n/a
 
      10-10-2007
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

 
Reply With Quote
 
 
 
 
Eric Hodel
Guest
Posts: n/a
 
      10-11-2007
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



 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      10-11-2007
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

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      10-11-2007
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/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      10-11-2007
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/.

 
Reply With Quote
 
Konrad Meyer
Guest
Posts: n/a
 
      10-11-2007
--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 <(E-Mail Removed)> 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)

iD8DBQBHDZekCHB0oCiR2cwRAoIiAJ4zDtSb9Vkz1s0O4fASSQ 7cDQnyHACgznWs
oVfN+75m30PrtFH2tlbGIHo=
=5nKb
-----END PGP SIGNATURE-----

--nextPart1742221.jO4t3LgaTi--

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      10-11-2007
On Oct 10, 9:25 pm, 7stud -- <(E-Mail Removed)> 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

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      10-11-2007
On 10/10/07, Eric Hodel <(E-Mail Removed)> 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

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      10-11-2007
On 10/10/07, Konrad Meyer <(E-Mail Removed)> 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

 
Reply With Quote
 
Eric Hodel
Guest
Posts: n/a
 
      10-11-2007

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Does recursive call able to print in same page as main call Yohan N. Leder Perl Misc 19 07-02-2006 11:45 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
Why does "while (<>) { $x .= $_; } print $x . "\n";" print all input lines? Wolfgang Perl Misc 1 02-13-2004 07:51 PM



Advertisments