P vs. print

On Fri, Sep 03, 2010 at 04:40:45AM +0900, Quintus wrote:

irb(main):003:0> print [1, 2, 3]
[1, 2, 3]=> nil

Are you sure about that? I get this:

irb(main):001:0> print [1, 2, 3]
123=> nil

The result you showed should probably be produced by:

print [1, 2, 3].inspect

Thanks @Brian for this nice clarification.

You mean here (inspect) right?

“Other classes inherit this from Object, although they can override it
if they wish.”

On Sep 4, 2010, at 12:02 PM, Chad P. wrote:

On Fri, Sep 03, 2010 at 04:40:45AM +0900, Quintus wrote:

irb(main):003:0> print [1, 2, 3]
[1, 2, 3]=> nil

Are you sure about that? I get this:

irb(main):001:0> print [1, 2, 3]
123=> nil

The result you showed should probably be produced by:

print [1, 2, 3].inspect


Chad P. [ original content licensed OWL: http://owl.apotheon.org ]

It all depends on what ruby you’re using:

$ rvm ruby -e ‘a=[1,2,3];puts “to_s”,a.to_s; puts “inspect”,a.inspect’
info: jruby-1.5.1: jruby 1.5.1 (ruby 1.8.7 patchlevel 249) (2010-06-06
f3a3480) (Java HotSpot™ Client VM 1.5.0_24) [i386-java]

Unable to find a $JAVA_HOME at “/usr”, continuing with system-provided
Java…
to_s
123
inspect
[1, 2, 3]

info: ree-1.8.7-head: ruby 1.8.7 (2009-06-12 patchlevel 174) [i686- darwin9.8.0]

to_s
123
inspect
[1, 2, 3]

info: ruby-1.8.6-p383: ruby 1.8.6 (2009-08-04 patchlevel 383) [i686- darwin9.8.0]

to_s
123
inspect
[1, 2, 3]

info: ruby-1.9.1-p378: ruby 1.9.1p378 (2010-01-10 revision 26273)
[i386-darwin9.8.0]

to_s
[1, 2, 3]
inspect
[1, 2, 3]

info: ruby-1.9.2-p0: ruby 1.9.2p0 (2010-08-18 revision 29036) [i386- darwin9.8.0]

to_s
[1, 2, 3]
inspect
[1, 2, 3]

So you might both be “right” on this.

-Rob

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

On Sat, Sep 4, 2010 at 11:02 AM, Chad P. [email protected] wrote:

On Fri, Sep 03, 2010 at 04:40:45AM +0900, Quintus wrote:

irb(main):003:0> print [1, 2, 3]
[1, 2, 3]=> nil

Are you sure about that? I get this:

irb(main):001:0> print [1, 2, 3]
123=> nil

The result you showed should probably be produced by:

print [1, 2, 3].inspect


Chad P. [ original content licensed OWL: http://owl.apotheon.org ]

Differences in versions. You can get an account at
http://ruby-versions.net/and play around like this

$ rvm ruby-1.8.6-p399,ruby-1.8.7-p174,ruby-1.9.1-p378,ruby-1.9.2-p0 -e
‘print [1,2,3]’

info: ruby-1.8.6-p399: ruby 1.8.6 (2010-02-05 patchlevel 399)
[x86_64-linux]

123
info: ruby-1.8.7-p174: ruby 1.8.7 (2009-06-12 patchlevel 174)
[x86_64-linux]

123
info: ruby-1.9.1-p378: ruby 1.9.1p378 (2010-01-10 revision 26273)
[x86_64-linux]

[1, 2, 3]
info: ruby-1.9.2-p0: ruby 1.9.2p0 (2010-08-18 revision 29036)
[x86_64-linux]

[1, 2, 3]

Going back further, it has been ‘123’ as far back as Ruby 1.0

$ ruby-1.0 -e ‘print [1,2,3]’
123