Detecting default used in method calls

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-forum.com/.

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

···

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?

Peter Lynch wrote:

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)"

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

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! :frowning:

--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-forum.com/\.

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

My blogs:

http://www.rubyinpractice.com/

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

···

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?

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

I LIT YOUR GEM ON FIRE!

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

I LIT YOUR GEM ON FIRE!

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

I LIT YOUR GEM ON FIRE!

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

···

On Wed, 3 Jan 2007, 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?

--
if you find yourself slandering anybody, first imagine that your mouth is
filled with excrement. it will break you of the habit quickly enough. - the
dalai lama

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

···

On 02.01.2007 17:20, Florian Gross wrote:

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.

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.