Detecting default used in method calls

P

Peter Lynch

I would like to know if a function has been called with or without an
optional argument.

If I have -

def the_Function (a, b = '')
end

and I invoke it like this -

the_Function('a')
the_Function('a', '')

is there any way for me to tell within the_Function which form of
invocation was used?
 
M

Mat Schaffer

I would like to know if a function has been called with or without an
optional argument.

If I have -

def the_Function (a, b = '')
end

and I invoke it like this -

the_Function('a')
the_Function('a', '')

is there any way for me to tell within the_Function which form of
invocation was used?

Not sure if there's something more graceful, but my first instinct
would be to define it like

def the_function(*args)
...
end

Then the function could contain the defaulting logic in the event of
only one element in the args array.
-Mat
 
F

Florian Gross

Peter said:
I would like to know if a function has been called with or without an
optional argument.

Not very pretty:

def fun(a, b = (no_b = true; 5))
if no_b then
"Fun(%p): %p" % [a, b]
else
"Fun(%p, %p)" % [a, b]
end
end

fun(1) # => "Fun(1): 5"
fun(1, 2) # => "Fun(1, 2)"
 
P

Phrogz

Peter said:
I would like to know if a function has been called with or without an
optional argument.

def foo( a, b=nil )
if b.nil?
# whoa
b=''
end
# ...
end
 
J

Jeremy McAnally

def myfunction(a, b = nil)
if (b):
puts "There's a B!"
else
puts "Business as usual..."
end
end

If you have more than one argument, then do something like this...

def myfunction(args={})

a = args[:a] || 'my default A'

b = args[:b] || 'my default B'

# and so on...
end

myfunction:)a => 'b', :b => 'c')

You can do this with merge too (i.e., build a hash with the keys A, B,
and so on, and then merge it with the argument).

I think there's a more graceful way to do this, but I'm too tired to find it! :(

--Jeremy

I would like to know if a function has been called with or without an
optional argument.

If I have -

def the_Function (a, b = '')
end

and I invoke it like this -

the_Function('a')
the_Function('a', '')

is there any way for me to tell within the_Function which form of
invocation was used?


--
My free Ruby e-book:
http://www.humblelittlerubybook.com/book/

My blogs:
http://www.mrneighborly.com/
http://www.rubyinpractice.com/
 
R

Robert Klemme

Not very pretty:

But clever!
def fun(a, b = (no_b = true; 5))
if no_b then
"Fun(%p): %p" % [a, b]
else
"Fun(%p, %p)" % [a, b]
end
end

fun(1) # => "Fun(1): 5"
fun(1, 2) # => "Fun(1, 2)"

The only other reasonable alternative I can see is this:

def fun(a,*bb)
if bb.empty?
puts "no b"
else
puts "b=#{b}"
end
end

Note that the other approach that has been mentioned cannot reliably
detect whether the parameter was set or not:

def fun(a,b=nil)
if b.nil?
puts "no b"
else
puts "b=#{b}"
end
end

irb(main):013:0> fun 1
no b
=> nil
irb(main):014:0> fun 1,2
b=2
=> nil
irb(main):015:0> fun 1,nil
no b
=> nil

(The last one should have printed "b=".)

You get more options if you want to use named parameters (i.e. a Hash):

def fun(args={})
a = args[:a]
b = args[:b]

if args.has_key? :b
puts "b=#{b}"
else
puts "no b"
end
end

irb(main):053:0> fun:)a=>1)
no b
=> nil
irb(main):054:0> fun:)a=>1, :b=>2)
b=2
=> nil
irb(main):055:0> fun:)b=>2)
b=2
=> nil

Kind regards

robert
 
E

Eric Hodel

I would like to know if a function has been called with or without an
optional argument.

If I have -

def the_Function (a, b = '')
end

and I invoke it like this -

the_Function('a')
the_Function('a', '')

is there any way for me to tell within the_Function which form of
invocation was used?

No need to do those complicated things that everybody else is
trying. Instead use an object that nobody else will pass you as a
sentinel:

class X

SENTINEL = Object.new

def the_function(a, b = SENTINEL)
if b == SENTINEL then
...
end
end

end

--
Eric Hodel - (e-mail address removed) - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!
 
A

ara.t.howard

I would like to know if a function has been called with or without an
optional argument.

If I have -

def the_Function (a, b = '')
end

and I invoke it like this -

the_Function('a')
the_Function('a', '')

is there any way for me to tell within the_Function which form of
invocation was used?

def the_function *a, &b
case a.size
when 0
p 'zero args'
when 1
p 'one arg'
when 2
p 'two args'
else
raise ArgumentError
end

# ...
end


but methods like this are often confusing to read

the_function 'arg'

the_function 'arg', 'wtf does this do?'

it's often better and more rubyish to use options

def the_function arg, opts = {}, &block
b = opts['b'] || opts[:b]
# ....
end


regards.


-a
 
T

Trans

Eric said:
No need to do those complicated things that everybody else is
trying. Instead use an object that nobody else will pass you as a
sentinel:

class X

SENTINEL = Object.new

def the_function(a, b = SENTINEL)
if b == SENTINEL then
...
end
end

end

You don't need to create your own, Exception works quite well:

class X

def the_function(a, b = Exception)
if b == Exception then
...
end
end

end

T.
 
T

Trans

Florian said:
Not very pretty:

def fun(a, b = (no_b = true; 5))
if no_b then
"Fun(%p): %p" % [a, b]
else
"Fun(%p, %p)" % [a, b]
end
end

fun(1) # => "Fun(1): 5"
fun(1, 2) # => "Fun(1, 2)"

Huh... That's pretty cool. Maybe prettier is we reduce it even more?

def fun(a, b = (_b = 5))
if _b then
"Fun(%p): %p" % [a, b]
else
"Fun(%p, %p)" % [a, b]
end
end

fun(1) # => "Fun(1): 5"
fun(1, 2) # => "Fun(1, 2)"

Think that works as long as b doesn't need to default to nil or false.

Oh... and I'm sitting here pronouncing _b as "un-b".

T.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top