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.

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:13 am, Trans <transf...@gmail.com> wrote:

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"

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.

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, 9:00 am, Phrogz <phr...@mac.com> wrote:

On Aug 27, 9:13 am, Trans <transf...@gmail.com> 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.

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

···

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