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

···

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

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

···

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?

--
Lars Haugseth

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

Cheers

robert

···

On Thu, Jul 5, 2012 at 10:29 AM, smoothedatol412 @gmail.com <lists@ruby-forum.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? 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??

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

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

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.

···

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

Basically, yes.

-- Matma Rex

···

2012/7/5 smoothedatol412 @gmail.com <lists@ruby-forum.com>:

So are the following two statements equivalent?

puts object.insect()
p object

Not very useful :stuck_out_tongue:

You can also do this in Pry:

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

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

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 Haugseth wrote in post #1067498:

···

From: io.c (C Method):
From: io.c (C Method):

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)

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

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 Sun, Jul 8, 2012 at 11:39 AM, smoothedatol412 @gmail.com <lists@ruby-forum.com> wrote:

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

see also

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

(you need require and/or gems for those)

···

On Thu, Jul 5, 2012 at 3:14 AM, Bartosz Dziewoński <matma.rex@gmail.com> wrote:

So are the following two statements equivalent?

puts object.inspect()
p object

Basically, yes.

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.

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

···

On 9 July 2012 16:09, Robert Klemme <shortcutter@googlemail.com> wrote:

--
  Matthew Kerwin, 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