p vs. print

  • Thread starter Abder-Rahman Ali
  • Start date
A

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
 
Q

Quintus

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

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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

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

Colin Bartlett

[Note: parts of this message were removed to make it a legal post.]

Am 02.09.2010 21:23, schrieb Alex Stahl:
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"
 
A

Abder-Rahman Ali

Alex said:
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).
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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

Alex Stahl

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

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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

Abder-Rahman Ali

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

Brian Candler

Abder-Rahman Ali said:
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
 
A

Abder-Rahman Ali

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

Brian Candler

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

Abder-Rahman Ali

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top