P vs. print

I wrote the following scripts from section (2) here:
http://ruby.gfd-dennou.org/tutorial/gokuraku/index-e.html

require "rubygems" # Not in the example
require "narray"
ary1 = NArray.sfloat(3,4)

When I tried to display the result using

print ary1

It did NOT show anything.

While when I used:

p ary1

I got the result displayed?

Why is that?

Thanks.

···

--
Posted via http://www.ruby-forum.com/.

Ruby uses "puts", not "print". "p" is short for "puts".

Try this:

puts ary1

You'll get the same results as:

p ary1

···

On Thu, 2010-09-02 at 14:16 -0500, Abder-Rahman Ali wrote:

I wrote the following scripts from section (2) here:
Dennou-Ruby Tutorial

require "rubygems" # Not in the example
require "narray"
ary1 = NArray.sfloat(3,4)

When I tried to display the result using

print ary1

It did NOT show anything.

While when I used:

p ary1

I got the result displayed?

Why is that?

Thanks.

Abder-Rahman Ali wrote:

When I tried to display the result using

print ary1

It did NOT show anything.

print(x) calls x.to_s to get the string representation, then sends that
to $stdout.

It looks like the object you have returns an empty string or nil from
its to_s method (which is odd, but you'd have to talk to the author of
the NArray class about that)

puts(x) is like print(x), but adds a newline if there isn't one at the
end of the string already.

p(x) is like puts(x.inspect), where x converts an object into a
debugging string form, that is, one which shows its internal state.

Try this program to see the differences:

class Foo
  def initialize(a)
    @a = a
  end
  def to_s
    "here's a string"
  end
end

f = Foo.new(123)
print f
puts f
p f

···

--
Posted via http://www.ruby-forum.com/\.

This is definitely wrong. #p is quite another method than #puts and yet
another than #print. Look at this:

irb(main):001:0> puts [1, 2, 3]
1
2
3
=> nil
irb(main):002:0> p [1, 2, 3]
[1, 2, 3]
=> [1, 2, 3]
irb(main):003:0> print [1, 2, 3]
[1, 2, 3]=> nil
irb(main):004:0>

Vale,
Marvin

···

Am 02.09.2010 21:23, schrieb Alex Stahl:

Ruby uses "puts", not "print". "p" is short for "puts".

Try this:

puts ary1

You'll get the same results as:

p ary1

Alex Stahl wrote:

Ruby uses "puts", not "print". "p" is short for "puts".

Try this:

puts ary1

You'll get the same results as:

p ary1

Thanks Alex. Actually, if I use:

puts ary1

I do NOT get an output (Empty).

···

--
Posted via http://www.ruby-forum.com/\.

Let IRB speak for himself:

···

****

x = [2,4,1]

=> [2, 4, 1]

p x

[2, 4, 1]
=> nil

puts x

2
4
1
=> nil

print x

241=> nil

p x.inspect

"[2, 4, 1]"
=> nil

puts x.inspect

[2, 4, 1]
=> nil

print x.inspect

[2, 4, 1]=> nil

p x.to_s

"241"
=> nil

puts x.to_s

241
=> nil

print x.to_s

241=> nil
****

p x = puts x.inspect

ceekays

Brian Candler wrote:

Abder-Rahman Ali wrote:

When I tried to display the result using

print ary1

It did NOT show anything.

print(x) calls x.to_s to get the string representation, then sends that
to $stdout.

It looks like the object you have returns an empty string or nil from
its to_s method (which is odd, but you'd have to talk to the author of
the NArray class about that)

puts(x) is like print(x), but adds a newline if there isn't one at the
end of the string already.

p(x) is like puts(x.inspect), where x converts an object into a
debugging string form, that is, one which shows its internal state.

Try this program to see the differences:

class Foo
  def initialize(a)
    @a = a
  end
  def to_s
    "here's a string"
  end
end

f = Foo.new(123)
print f
puts f
p f

--
Posted via http://www.ruby-forum.com/\.

@Brian.

Thanks for your reply. That makes it more clear.

This is the output I got from running your script:

C:\Users\Abder-Rahman\Desktop\Research>ruby p_vs_print_vs_puts.rb
here's a stringhere's a string
#<Foo:0x2c25930 @a=123>

Can you just describe this part: #<Foo:0x2c25930 @a=123>

And, when you said: "p(x) is like puts(x.inspect), [[where x converts an
object]] into a debugging string form, that is, one which shows its
internal state."

Shouldn't "... where x converts an object...]], be "... where "inspect"
converts an object..."?

Thanks.

···

--
Posted via http://www.ruby-forum.com/.

@Alex & @Ali The correct explanation is P is effectively puts x.inspect

Notice in the script I sent Ali I used inspect I in the printing of ary1 when using puts.

[-------------------------]
require "rubygems"
require "narray"

ary1 = NArray.sfloat(3,4)
puts "---puts ary1.inspect produces"
puts ary1.inspect
puts "---p ary1 produces"
p ary1
puts "---puts ary1 produces"
puts ary1
puts "---"

[------------------------]

produces:
---puts ary1.inspect produces
NArray.sfloat(3,4):
[ [ 0.0, 0.0, 0.0 ],
  [ 0.0, 0.0, 0.0 ],
  [ 0.0, 0.0, 0.0 ],
  [ 0.0, 0.0, 0.0 ] ]
---p ary1 produces
NArray.sfloat(3,4):
[ [ 0.0, 0.0, 0.0 ],
  [ 0.0, 0.0, 0.0 ],
  [ 0.0, 0.0, 0.0 ],
  [ 0.0, 0.0, 0.0 ] ]
---puts ary1 produces

···

---

________________________________
From: Alex Stahl <astahl@hi5.com>
Reply-To: <ruby-talk@ruby-lang.org>
Date: Thu, 2 Sep 2010 14:23:57 -0500
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Subject: Re: p vs. print

Ruby uses "puts", not "print". "p" is short for "puts".

Try this:

puts ary1

You'll get the same results as:

p ary1

On Thu, 2010-09-02 at 14:16 -0500, Abder-Rahman Ali wrote:

I wrote the following scripts from section (2) here:
Dennou-Ruby Tutorial

require "rubygems" # Not in the example
require "narray"
ary1 = NArray.sfloat(3,4)

When I tried to display the result using

print ary1

It did NOT show anything.

While when I used:

p ary1

I got the result displayed?

Why is that?

Thanks.

Actually, "p x" is equivalent to "puts x.inspect", not "puts x"

···

On Thu, Sep 2, 2010 at 3:40 PM, Quintus <sutniuq@gmx.net> wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 02.09.2010 21:23, schrieb Alex Stahl:
> Ruby uses "puts", not "print". "p" is short for "puts".
>
> Try this:
>
> puts ary1
>
> You'll get the same results as:
>
> p ary1
>
>

This is definitely wrong. #p is quite another method than #puts and yet
another than #print. Look at this:

irb(main):001:0> puts [1, 2, 3]
1
2
3
=> nil
irb(main):002:0> p [1, 2, 3]
[1, 2, 3]
=> [1, 2, 3]
irb(main):003:0> print [1, 2, 3]
[1, 2, 3]=> nil
irb(main):004:0>

Vale,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkx//a4ACgkQDYShvwAbcNm6WACeIQJ9iRiMc0pWK2b5WLyyp0dF
w4cAnjhR1ivOKumpbjiIyiM3x5WXxH1O
=3hf3
-----END PGP SIGNATURE-----

> Ruby uses "puts", not "print". "p" is short for "puts".
> Try this:
> puts ary1
> You'll get the same results as:
> p ary1

This is definitely wrong. #p is quite another method than #puts and yet

another than #print. Look at this ...

And as another example of the differences:
class P
  def inspect(); "P#inspect"; end
  def to_s(); "P#to_s"; end
end

q = P.new
puts "p"
p q #=> "P#inspect"
puts "puts"
puts q #=> "P#to_s"
puts "print"
print q
puts ":: just after print"
              #=> "P#to_s:: just after print"

···

On Thu, Sep 2, 2010 at 8:40 PM, Quintus <sutniuq@gmx.net> wrote:

Am 02.09.2010 21:23, schrieb Alex Stahl:

What is "inspect"? What does it do?

···

--
Posted via http://www.ruby-forum.com/.

Thanks @Edmond for the clarification.

···

--
Posted via http://www.ruby-forum.com/.

Thanks @Savard.

···

--
Posted via http://www.ruby-forum.com/.

Abder-Rahman Ali wrote:

This is the output I got from running your script:

C:\Users\Abder-Rahman\Desktop\Research>ruby p_vs_print_vs_puts.rb
here's a stringhere's a string
#<Foo:0x2c25930 @a=123>

Can you just describe this part: #<Foo:0x2c25930 @a=123>

That's what Object#inspect returns: a string containing the class name,
the pointer to the object instance in memory, and the values of all
instance variables. Other classes inherit this from Object, although
they can override it if they wish.

And, when you said: "p(x) is like puts(x.inspect), [[where x converts an
object]] into a debugging string form, that is, one which shows its
internal state."

Shouldn't "... where x converts an object...]], be "... where "inspect"
converts an object..."?

Yes, my mistake.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

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

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

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

It just returns a user-friendly version of the object. It can be overridden
to do whatever you want, it's just a regular ol' method on Object.

···

On Thu, Sep 2, 2010 at 3:52 PM, Abder-Rahman Ali <abder.rahman.ali@gmail.com > wrote:

What is "inspect"? What does it do?
--
Posted via http://www.ruby-forum.com/\.

Sorry, my wording was off. Understood that they're different methods.

I think of them synonymously, and so when I say "short for", I mean
that's my way of thinking about how they accomplish similar tasks, not
that "p" is literally a representation of "puts".

Thanks for the clarification in any case.

···

On Thu, 2010-09-02 at 14:46 -0500, Colin Bartlett wrote:

On Thu, Sep 2, 2010 at 8:40 PM, Quintus <sutniuq@gmx.net> wrote:

> Am 02.09.2010 21:23, schrieb Alex Stahl:
> > Ruby uses "puts", not "print". "p" is short for "puts".
> > Try this:
> > puts ary1
> > You'll get the same results as:
> > p ary1
>
>
This is definitely wrong. #p is quite another method than #puts and yet
> another than #print. Look at this ...

And as another example of the differences:
class P
  def inspect(); "P#inspect"; end
  def to_s(); "P#to_s"; end
end

q = P.new
puts "p"
p q #=> "P#inspect"
puts "puts"
puts q #=> "P#to_s"
puts "print"
print q
puts ":: just after print"
              #=> "P#to_s:: just after print"

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

···

--
Posted via http://www.ruby-forum.com/.

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(TM) 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 Biedenharn
Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/
rab@GaslightSoftware.com http://GaslightSoftware.com/

···

On Sep 4, 2010, at 12:02 PM, Chad Perrin 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 Perrin [ 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

···

On Sat, Sep 4, 2010 at 11:02 AM, Chad Perrin <code@apotheon.net> 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 Perrin [ original content licensed OWL: http://owl.apotheon.org ]