[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Detecting default used in method calls

Peter Lynch

1/2/2007 3:31:00 PM

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?

--
Posted via http://www.ruby-....

9 Answers

Mat Schaffer

1/2/2007 4:05:00 PM

0


On Jan 2, 2007, at 10:31 AM, Peter Lynch wrote:

> 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


Florian Groß

1/2/2007 4:21:00 PM

0

Gavin Kistner

1/2/2007 4:33:00 PM

0

Peter Lynch wrote:
> 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

Jeremy McAnally

1/2/2007 4:39:00 PM

0

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

On 1/2/07, Peter Lynch <argnosis@yahoo.com.au> wrote:
> 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?
>
> --
> Posted via http://www.ruby-....
>
>


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

My blogs:
http://www.mrneigh...
http://www.rubyinpra...

Robert Klemme

1/2/2007 4:55:00 PM

0

On 02.01.2007 17:20, Florian Gross wrote:
> Peter Lynch wrote:
>
>> I would like to know if a function has been called with or without an
>> optional argument.
>
> 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

Eric Hodel

1/2/2007 7:36:00 PM

0

On Jan 2, 2007, at 07:31, Peter Lynch wrote:

> 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 - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!



--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!



--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Ara.T.Howard

1/2/2007 7:51:00 PM

0

Trans

1/2/2007 8:09:00 PM

0


Eric Hodel wrote:
> 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.


Trans

1/2/2007 8:17:00 PM

0

Florian Gross wrote:
> 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.