More convenient #p?

Has anyone else every wished #p would passthru it’s argument? Ie. Work
like this:

def p(x)
puts x.inspect
x
end

Is there any good reason that it shouldn’t? I noticed this was in
Ruby’s source code TODO list once, but it never came to pass for some
reason.

Thanks,
T.

On Aug 27, 9:13 am, Trans [email protected] wrote:

Has anyone else every wished #p would passthru it’s argument? Ie. Work
like this:

def p(x)
puts x.inspect
x
end

I can’t recall ever wanting that. (Though I did find it odd that it,
and puts, return nil for no seemingly good reason.)

Have you wished that, Trans? If so, I’d be interested in your use
case.

Is there any good reason that it shouldn’t?

I can’t think of any good reason, other than perhaps reserving the
return value for some more useful information in the future.

On Aug 27, 9:00 am, Phrogz [email protected] wrote:

I can’t recall ever wanting that. (Though I did find it odd that it,
and puts, return nil for no seemingly good reason.)

Have you wished that, Trans? If so, I’d be interested in your use
case.

Oh yes, lots of times. When I’m debugging code I sometimes want to see
the state of an object, but I don’t want to mess with the execution.
With complex statements this isn’t always straight forward. For
instance,

def foo(x)
bar(baz(x))
end

If I want to see the result of baz(x), I’d need to change it to
something like:

def foo(x)
p(r = baz(x))
bar(r)
end

But if #p passed thru, it’s simply:

def foo(x)
bar(p(baz(x)))
end

T.

On Aug 27, 10:14 am, WATANABE Hirofumi [email protected] wrote:

% ruby -ve ‘p “foo”.gsub(“o”){p “a”}’
ruby 1.9.0 (2007-08-28 patchlevel 0) [i386-linux]
“a”
“a”
“faa”

Yea!

T.

Well, the beauty of Ruby is that you can change that as you see fit!
But ther seems little reason for having a return value that matches
the value you want to output if you are outputting the value anyway.
The value hasn’t changed and is already located in it’s own little
object. Seems in Ruby it’s easy enough to pass that value to output
and to anywhere else you need it/ want it.

It’s made with C, but I don’t think it works like C.

Hi,

Trans wrote:

Has anyone else every wished #p would passthru it’s argument? Ie. Work
like this:

def p(x)
puts x.inspect
x
end

% ruby -ve ‘p “foo”.gsub(“o”){p “a”}’
ruby 1.9.0 (2007-08-28 patchlevel 0) [i386-linux]
“a”
“a”
“faa”

On 27.08.2007 19:02, Trans wrote:

Has anyone else every wished #p would passthru it’s argument? Ie. Work
like this:
def p(x)
puts x.inspect
x
end

Here is a version that also handles multiple parameters required for
correct automatic array building like in “a,b,c=p(1,2,3)”:

module Kernel
alias_method :old_p, :stuck_out_tongue:
def p *args
old_p(*args)
args.length>1 ? args : args[0]
end
end

If multiple parameters are given, let’s return the array we already
have. Don’t return an array if it’s just one parameter (the usual
case). Note that the case of no parameter results in nil returned
(certainly).

a=p(“Hi!”) # >> “Hi!”
b,c,d=p(“Lo!”,[3,4],:sym) # >> “Lo!”
# >> [3, 4]
# >> :sym
e=p([“Me!”]) # >> [“Me!”]
f=p # >> nil
p(a,b,c,d,e,f) # >> “Hi!”
# >> “Lo!”
# >> [3, 4]
# >> :sym
# >> [“Me!”]
# >> nil

Anyway, great idea! :slight_smile:

  • Matthias

Trans wrote:

Has anyone else every wished #p would passthru it’s argument? Ie. Work
like this:

def p(x)
puts x.inspect
x
end

Is there any good reason that it shouldn’t? I noticed this was in
Ruby’s source code TODO list once, but it never came to pass for some
reason.

Thanks,
T.

I prefer the following modification, because it makes it easy to be a
voyeur in method chains.

irb(main):001:0> module Kernel
irb(main):002:1> def my_p
irb(main):003:2> p self
irb(main):004:2> self
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> %w{a b c d}.my_p.collect{|i|’<’+i+’>’}.my_p.reverse
[“a”, “b”, “c”, “d”]
["", “”, “”, “”]
=> ["", “”, “”, “
”]

Wolfgang Nádasi-Donner

On Aug 28, 11:34 am, “Wolfgang Nádasi-Donner” [email protected]
wrote:

Ruby’s source code TODO list once, but it never came to pass for some
irb(main):003:2> p self

Posted viahttp://www.ruby-forum.com/.

Call me crazy, but isn’t this thread really about how great it would
be to use Object#tap?

http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions.html

On Aug 28, 10:55 am, Yossef M. [email protected] wrote:

puts x.inspect

I prefer the following modification, because it makes it easy to be a
[“a”, “b”, “c”, “d”]
http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-ex
Similar idea. But #p is for debugging. Who would want to go through
all the trouble of “.tap{ p … }.” when “p …” will do?

T.

Yossef M. wrote:

Call me crazy, but isn’t this thread really about how great it would
be to use Object#tap?

Yes, but it isn’t available before Ruby 1.9:

C:\Dokumente und Einstellungen\wolfgang>irb
irb(main):001:0> “abc”.tap{|x|p x}
NoMethodError: undefined method `tap’ for “abc”:String
from (irb):1
irb(main):002:0> exit

C:\Dokumente und Einstellungen\wolfgang>irb19
irb(main):001:0> “abc”.tap{|x|p x}
“abc”
=> “abc”

Wolfgang Nádasi-Donner