detecting default parameters

I

Ian Macdonald

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.
(e-mail address removed) |
http://www.caliban.org |
|
 
S

Simon Strandgaard

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?

server> ruby a.rb
42
42
666
server> expand -t2 a.rb
def test(value=nil)
value || 42
end
p test, test(nil), test(666)
server>
 
P

Phil Tomson

Ian Macdonald said:
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
 
N

nobu.nokada

Hi,

At Sat, 24 Jan 2004 02:45:07 +0900,
Ian said:
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
 
I

Ian Macdonald

def my_method(foo = (defaulted = 'bar'))
if defaulted
puts "warning: default value #{foo} being used for 'foo'"
end
end

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
--
Ian Macdonald | Hash table has woodworm
System Administrator |
(e-mail address removed) |
http://www.caliban.org |
|
 

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
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top