Variable argument number and behaviour [Newbie]

Discussion in 'Ruby' started by Ryan Allan, Jul 1, 2007.

  1. Ryan Allan

    Ryan Allan Guest

    Hello Everyone,

    I'm trying to make a function take arguments in a particular way.

    This is what I want to create:

    #Variable argument testing
    def function(*sometimes)

    if sometimes.nil?
    print "Not given!"
    else
    print "Here!"
    end

    end

    function
    [EOF]

    As I understand it, the * operator bundles the argument into an array,
    so that if test returns true. I know that I can use "if
    sometimes.length==0" to get the same behaviour, but that is rather
    obscure compared to the first version.

    Is there some other way to set up the argument list? (Alas, I have found
    nothing in the documentation or the Forum.)

    Thank you,
    -Ryan

    --
    Posted via http://www.ruby-forum.com/.
    Ryan Allan, Jul 1, 2007
    #1
    1. Advertising

  2. Alle domenica 1 luglio 2007, Ryan Allan ha scritto:
    > Hello Everyone,
    >
    > I'm trying to make a function take arguments in a particular way.
    >
    > This is what I want to create:
    >
    > #Variable argument testing
    > def function(*sometimes)
    >
    > if sometimes.nil?
    > print "Not given!"
    > else
    > print "Here!"
    > end
    >
    > end
    >
    > function
    > [EOF]
    >
    > As I understand it, the * operator bundles the argument into an array,
    > so that if test returns true. I know that I can use "if
    > sometimes.length==0" to get the same behaviour, but that is rather
    > obscure compared to the first version.
    >
    > Is there some other way to set up the argument list? (Alas, I have found
    > nothing in the documentation or the Forum.)
    >
    > Thank you,
    > -Ryan


    I can't think of any other way to make a method take any number of arguments,
    but remember that there's a shorter way to tell whether an array is empty:
    the empty? method:

    def function(*sometimes)
    if sometimes.empty?
    print "Not given!"
    else
    print "Here!"
    end
    end

    This way, it's as clear as what you wanted to use. Besides, even if it had
    been possible, your approach would have had a problem: you wouldn't have been
    able to distinguish the case of a single nil argument from the case of no
    arguments:

    function(nil)

    function()

    The correct approach solves this problem: if no arguments are passed,
    sometimes is empty (empty? returns true); otherwise it's not empty, even if
    all arguments are nil.

    I hope this helps

    Stefano
    Stefano Crocco, Jul 1, 2007
    #2
    1. Advertising

  3. Ryan Allan

    Guest

    On Jul 1, 4:53 pm, Ryan Allan <> wrote:
    > Hello Everyone,
    >
    > I'm trying to make a function take arguments in a particular way.
    >
    > This is what I want to create:
    >
    > #Variable argument testing
    > def function(*sometimes)
    >
    > if sometimes.nil?
    > print "Not given!"
    > else
    > print "Here!"
    > end
    >
    > end
    >
    > function
    > [EOF]
    >
    > As I understand it, the * operator bundles the argument into an array,
    > so that if test returns true. I know that I can use "if
    > sometimes.length==0" to get the same behaviour, but that is rather
    > obscure compared to the first version.
    >
    > Is there some other way to set up the argument list? (Alas, I have found
    > nothing in the documentation or the Forum.)
    >
    > Thank you,
    > -Ryan
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    I think you are looking for default arguments:

    def function(arg = nil)
    # do stuff
    end

    If no argument is passed to function, arg will be nil, else it will
    have the value passed in. Note you don't have to use nil for the
    default, you could use any other value as well (even an expression).

    However, if you still want to allow an arbitrary number of arguments,
    I think you'll have to use the *arg syntax and check the array length
    as Ruby doesn't allow:

    def f(*arg = nil)
    end

    Jeremy
    , Jul 1, 2007
    #3
  4. Ryan Allan

    Ryan Allan Guest

    Re: Variable argument number and behaviour

    Thank you Stefano.
    You're right; the empty? method is nicely obvious.
    I do have a further question, however. You've highlighted a problem
    with my original idea, but I don't understand it.

    > Besides, even if it
    > had been possible, your approach would have had a problem: you wouldn't have
    > been able to distinguish the case of a single nil argument from the case of
    > no arguments:
    >
    > function(nil)
    >
    > function()
    >
    > The correct approach solves this problem: if no arguments are passed,
    > sometimes is empty (empty? returns true); otherwise it's not empty, even
    > if all arguments are nil.


    What is the difference between no arguments and a single nil argument?
    It seems to me that the cases are logically equivalent. Ruby treats them
    separately?

    Thanks again,
    -Ryan

    --
    Posted via http://www.ruby-forum.com/.
    Ryan Allan, Jul 1, 2007
    #4
  5. Ryan Allan

    Guest

    Re: Variable argument number and behaviour

    Hi --

    On Mon, 2 Jul 2007, Ryan Allan wrote:

    > Thank you Stefano.
    > You're right; the empty? method is nicely obvious.
    > I do have a further question, however. You've highlighted a problem
    > with my original idea, but I don't understand it.
    >
    >> Besides, even if it
    >> had been possible, your approach would have had a problem: you wouldn't have
    >> been able to distinguish the case of a single nil argument from the case of
    >> no arguments:
    >>
    >> function(nil)
    >>
    >> function()
    >>
    >> The correct approach solves this problem: if no arguments are passed,
    >> sometimes is empty (empty? returns true); otherwise it's not empty, even
    >> if all arguments are nil.

    >
    > What is the difference between no arguments and a single nil argument?
    > It seems to me that the cases are logically equivalent. Ruby treats them
    > separately?


    Yes, because nil is an object.

    def meth(x)
    p x
    end

    meth(1) # 1
    meth(nil) # nil
    meth # ArgumentError: wrong number of arguments


    David

    --
    * Books:
    RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
    RUBY FOR RAILS (http://www.manning.com/black)
    * Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
    , Jul 1, 2007
    #5
  6. Ryan Allan

    Ryan Allan Guest

    Re: Variable argument number and behaviour

    Thank you David. That makes much more sense now.

    --
    Posted via http://www.ruby-forum.com/.
    Ryan Allan, Jul 1, 2007
    #6
  7. Ryan Allan

    Vu Nang Luc Guest

    Re: Variable argument number and behaviour


    > Yes, because nil is an object.
    >

    Right, and if you use Array.nil? passing no argument still return
    false as following:

    > a = []
    > a.nil?

    =>false
    Vu Nang Luc, Jul 2, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrej Prsa

    Variable argument number in functions

    Andrej Prsa, Sep 4, 2003, in forum: C Programming
    Replies:
    3
    Views:
    311
    Ben Pfaff
    Sep 4, 2003
  2. Andrej Prsa

    Pointer-to-function with variable argument number

    Andrej Prsa, Mar 4, 2004, in forum: C Programming
    Replies:
    5
    Views:
    474
    Derk Gwen
    Mar 4, 2004
  3. Ben Kial
    Replies:
    1
    Views:
    648
    Eric Enright
    Nov 15, 2004
  4. S?ren Gammelmark
    Replies:
    1
    Views:
    1,884
    Eric Sosman
    Jan 7, 2005
  5. AikidoGuy
    Replies:
    11
    Views:
    558
    Seebs
    Nov 21, 2011
Loading...

Share This Page