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.

Ruby uses “puts”, not “print”. “p” is short for “puts”.

Try this:

puts ary1

You’ll get the same results as:

p ary1

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

Am 02.09.2010 21:23, schrieb Alex S.:

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

On Thu, Sep 2, 2010 at 8:40 PM, Quintus [email protected] wrote:

Am 02.09.2010 21:23, schrieb Alex S.:

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”

Alex S. 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).

What is “inspect”? What does it do?

Actually, “p x” is equivalent to “puts x.inspect”, not “puts x”

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 A.
<[email protected]

No problem. I actually used to think that they were identical, which led
to
some confusion. That’s the only reason i point it out.

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.

Thanks everyone for the clarification.

@Andrew. When you say: “…it’s just a regular ol’ method on Object.”.

What do you mean by “ol’”?

Thanks.

Not to mention pp which is prettyprint
http://ruby-doc.org/stdlib/libdoc/pp/rdoc/index.html
And there is ap which is awesomeprint
http://github.com/michaeldv/awesome_print

MarkT

Abder-Rahman A. 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

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 C. wrote:

Abder-Rahman A. 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

Thanks @Edmond for the clarification.

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

Mark T wrote:

Not to mention pp which is prettyprint
http://ruby-doc.org/stdlib/libdoc/pp/rdoc/index.html
And there is ap which is awesomeprint
http://github.com/michaeldv/awesome_print

MarkT

That’s nice.

Thanks @Savard.

Abder-Rahman A. 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.

@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 S. [email protected]
Reply-To: [email protected]
Date: Thu, 2 Sep 2010 14:23:57 -0500
To: ruby-talk ML [email protected]
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs