Matthias Wächter
8/27/2007 9:09:00 PM
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, :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