More convenient #p?

T

Trans

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

Phrogz

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

Trans

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

WATANABE Hirofumi

Hi,
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"
 
J

John Joyce

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

Matthias Wächter

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 :eek:ld_p, :p
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! :)

- Matthias
 
W

Wolfgang Nádasi-Donner

Trans said:
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"]
["<a>", "<b>", "<c>", "<d>"]
=> ["<d>", "<c>", "<b>", "<a>"]

Wolfgang Nádasi-Donner
 
Y

Yossef Mendelssohn

Trans said:
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
=3D> nil
irb(main):007:0> %w{a b c d}.my_p.collect{|i|'<'+i+'>'}.my_p.reverse
["a", "b", "c", "d"]
["<a>", "<b>", "<c>", "<d>"]
=3D> ["<d>", "<c>", "<b>", "<a>"]

Wolfgang N=E1dasi-Donner

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-expressi=
ons.html
 
W

Wolfgang Nádasi-Donner

Yossef said:
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
 
T

Trans

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
=3D> nil
irb(main):007:0> %w{a b c d}.my_p.collect{|i|'<'+i+'>'}.my_p.reverse
["a", "b", "c", "d"]
["<a>", "<b>", "<c>", "<d>"]
=3D> ["<d>", "<c>", "<b>", "<a>"]
Wolfgang N=E1dasi-Donner

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-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=2E
 

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

Forum statistics

Threads
474,264
Messages
2,571,318
Members
48,003
Latest member
coldDuece

Latest Threads

Top