Question: p expression

Lately I have been going through some Ruby books and I keep coming up on
an script like the following:

hash={“A”=>10, “B”=>20, “C”=>30}
p hash.keys() # what is this

What is this p method or expression I keep coming across? I understand
that this is some kind of print statement, but that is all I understand
about this statement.
I have tried doing a google search, but since I cannot defined what
this expression is I have not found a vaild result. Can someone please
explain what this [p] expression or method is??

Happy Coding

On 07/05/2012 10:29 AM, smoothedatol412 @gmail.com wrote:

Lately I have been going through some Ruby books and I keep coming up on
an script like the following:

hash={“A”=>10, “B”=>20, “C”=>30}
p hash.keys() # what is this

What is this p method or expression I keep coming across?

[1] pry(main)> method(:p)
=> #<Method: Object(Kernel)#p>

So are the following two statements equivalent?

puts object.insect()
p object

Its just a short hand version of [puts object.insect()]? I want to ask
but I start using it in notes or in code, and I have been learning or
thing its one thing when it is another…

2012/7/5 smoothedatol412 @gmail.com [email protected]:

So are the following two statements equivalent?

puts object.insect()
p object

Basically, yes.

– Matma R.

Thanks alot

On Thu, Jul 5, 2012 at 10:29 AM, smoothedatol412 @gmail.com
[email protected] wrote:

this expression is I have not found a vaild result. Can someone please
explain what this [p] expression or method is??

Try “ri p” instead or http://rdoc.info/stdlib/core/frames

Cheers

robert

Not very useful :stuck_out_tongue:

You can also do this in Pry:

[37] (pry) main: 0> ? p

From: io.c (C Method):
Number of lines: 11
Owner: Kernel
Visibility: private
Signature: p(*arg1)

For each object, directly writes
obj.inspect followed by the current output
record separator to the program’s standard output.

S = Struct.new(:name, :state)
s = S[‘dave’, ‘TX’]
p s

produces:

#<S name=“dave”, state=“TX”>
[38] (pry) main: 0> $ p

From: io.c (C Method):
Number of lines: 20
Owner: Kernel
Visibility: private

static VALUE
rb_f_p(int argc, VALUE *argv, VALUE self)
{
int i;
VALUE ret = Qnil;

for (i=0; i<argc; i++) {
rb_p(argv[i]);
}
if (argc == 1) {
ret = argv[0];
}
else if (argc > 1) {
ret = rb_ary_new4(argc, argv);
}
if (TYPE(rb_stdout) == T_FILE) {
rb_io_flush(rb_stdout);
}
return ret;

}
[39] (pry) main: 0>

Lars H. wrote in post #1067498:

On 07/05/2012 10:29 AM, smoothedatol412 @gmail.com wrote:

Lately I have been going through some Ruby books and I keep coming up on
an script like the following:

hash={“A”=>10, “B”=>20, “C”=>30}
p hash.keys() # what is this

What is this p method or expression I keep coming across?

[1] pry(main)> method(:p)
=> #<Method: Object(Kernel)#p>

Module: Kernel (Ruby 1.9.3)

I did not know that people were still commenting this topic…
Anyways, I do not like the short hand version of using this type of
statement in ruby when I tried to use it in test scripts.

short example

var=10
p var

I have choose to use the following statement instead

var=10
puts var.inspect()

I have come from Java and a few other programming backgrounds and I am
use to the compiled langs, like C and Java. I like coding things
out the long way since it is easy to go back later on and easily find
that bug code that is creating problems for the program.

On Sun, Jul 8, 2012 at 11:39 AM, smoothedatol412 @gmail.com
[email protected] wrote:

var=10
puts var.inspect()

I have come from Java and a few other programming backgrounds and I am
use to the compiled langs, like C and Java. I like coding things
out the long way since it is easy to go back later on and easily find
that bug code that is creating problems for the program.

While I generally sympathize with that approach (making things
explicit) I don’t agree in this particular case. You’ll find “p var”
as easily as “puts var.inspect” - but it’s far less typing.

Cheers

robert

On Thu, Jul 5, 2012 at 3:14 AM, Bartosz Dziewoński [email protected]
wrote:

So are the following two statements equivalent?

puts object.inspect()
p object

Basically, yes.

see also

pp (pretty print)
ap (awesome print)
d (debug)
g (growl)

(you need require and/or gems for those)

On Sun, Jul 8, 2012 at 11:39 AM, smoothedatol412 @gmail.com

I have come from Java and a few other programming backgrounds and I am
use to the compiled langs, like C and Java. I like coding things
out the long way since it is easy to go back later on and easily find
that bug code that is creating problems for the program.

On 9 July 2012 16:09, Robert K. [email protected] wrote:

While I generally sympathize with that approach (making things
explicit) I don’t agree in this particular case. You’ll find “p var”
as easily as “puts var.inspect” - but it’s far less typing.

Not to mention the convenience (with a single extra require) of
changing “p var” to “pp var”, especially in the absence of
var.pretty_inspect


Matthew K., B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651

“You’ll never find a programming language that frees
you from the burden of clarifying your ideas.” - xkcd