Hello,
Does anyone know of a way to detect whether default parameters are being
used in a method? I’m looking for something like the pseudo-method
’default?’ in the following example:
def my_method(foo=‘bar’)
if foo.default?
puts "warning: default value #{foo} being used for ‘foo’"
end
end
I suspect Ruby no longer holds any state at this point to reflect the
fact that the caller did not supply a parameter, but I’m hoping there’s
some way to do it.
Ian
···
–
Ian Macdonald | When the wind is great, bow before it; when
System Administrator | the wind is heavy, yield to it.
ian@caliban.org |
http://www.caliban.org |
>
Does anyone know of a way to detect whether default parameters are being
used in a method? I’m looking for something like the pseudo-method
‘default?’ in the following example:
[snip]
Have you considered using ‘nil’ as default value?
ruby a.rb
42
42
666
expand -t2 a.rb
def test(value=nil)
value || 42
end
p test, test(nil), test(666)
···
On Sat, 24 Jan 2004 02:45:07 +0900, Ian Macdonald wrote:
–
Simon Strandgaard
Ian Macdonald ian@caliban.org wrote in message news:20040123174504.GY27623@caliban.org…
Hello,
Does anyone know of a way to detect whether default parameters are being
used in a method? I’m looking for something like the pseudo-method
‘default?’ in the following example:
def my_method(foo=‘bar’)
if foo.default?
puts “warning: default value #{foo} being used for ‘foo’”
end
end
I suspect Ruby no longer holds any state at this point to reflect the
fact that the caller did not supply a parameter, but I’m hoping there’s
some way to do it.
I doubt there would be any way to actually implement #default? as you show.
You could do it this way, I suppose, but it’s probably not what you’re looking for:
def my_method(foo=‘bar’)
if(foo==‘bar’)
puts “warning: default value #{foo} being used for ‘foo’”
end
end
What is it you’re trying to accomplish? Perhaps there’s a different way to do it.
Phil
Hi,
Does anyone know of a way to detect whether default parameters are being
used in a method? I’m looking for something like the pseudo-method
‘default?’ in the following example:
def my_method(foo=‘bar’)
if foo.default?
puts “warning: default value #{foo} being used for ‘foo’”
end
end
def my_method(*args)
if args.empty?
foo = ‘bar’
puts “warning: default value #{foo} being used for ‘foo’”
elsif args.size > 1
raise ArgumentError, “wrong number of arguments (#{args.size} of 1)”
else
foo = *args
end
end
def my_method(foo = (defaulted = ‘bar’))
if defaulted
puts “warning: default value #{foo} being used for ‘foo’”
end
end
···
At Sat, 24 Jan 2004 02:45:07 +0900, Ian Macdonald wrote:
–
Nobu Nakada
def my_method(foo = (defaulted = ‘bar’))
if defaulted
puts “warning: default value #{foo} being used for ‘foo’”
end
end
I like that. Nice and simple, and easily extended:
def my_method(foo = (default_foo = ‘foo’), bar = (default_bar = ‘bar’))
warn “warning: default value #{foo} being used for foo” if default_foo;
warn “warning: default value #{bar} being used for bar” if default_bar;
. . .
end
or, to get a little fancier:
def my_method(foo = ((defaulted||={})[:foo] = ‘foo’),
bar = ((defaulted||={})[:bar] = ‘bar’))
if defaulted
defaulted.each do
>arg, val|
warn “warning: default value #{val}' being used for
#{arg}'”
end
end
end
irb(main):001:0> my_method
warning: default value bar' being used for
bar’
warning: default value foo' being used for
foo’
irb(main):002:0> my_method(1)
warning: default value bar' being used for
bar’
irb(main):003:0> my_method(1,2)
irb(main):004:0>
···
On Sat, Jan 24, 2004 at 02:03:52PM +0900, nobu.nokada@softhome.net wrote:
Aha, very clever!
When I first looked at this, it made perfect sense, but the longer I
stare at it, the less obvious it seems to me and the more it makes my
brain itch, as Hal would say.
Ian
···
On Sat 24 Jan 2004 at 14:03:52 +0900, nobu.nokada@softhome.net wrote:
def my_method(foo = (defaulted = ‘bar’))
if defaulted
puts “warning: default value #{foo} being used for ‘foo’”
end
end
–
Ian Macdonald | Hash table has woodworm
System Administrator |
ian@caliban.org |
http://www.caliban.org |
>