Default parameter values for methods in Ruby 1.9

Discussion in 'Ruby' started by Iñaki Baz Castillo, Jul 21, 2009.

  1. Hi, I expected that the following is valid in Ruby 1.9:

    def get_element(auid, document=3Dnil, selector, check_etag=3Dtrue)
    [...]
    end

    However I get an error:

    syntax error, unexpected '=3D', expecting ')' (SyntaxError) ...,
    element_selector, check_etag=3Dtrue)
    ^

    Thanks for any tip.



    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Jul 21, 2009
    #1
    1. Advertising

  2. Iñaki Baz Castillo wrote:
    > Hi, I expected that the following is valid in Ruby 1.9:
    >
    > def get_element(auid, document=nil, selector, check_etag=true)
    > [...]
    > end


    What do you expect

    get_element(1,2,3)

    to do?

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Jul 21, 2009
    #2
    1. Advertising

  3. El Martes, 21 de Julio de 2009, Joel VanderWerf escribi=C3=B3:
    > I=C3=B1aki Baz Castillo wrote:
    > > Hi, I expected that the following is valid in Ruby 1.9:
    > >
    > > def get_element(auid, document=3Dnil, selector, check_etag=3Dtrue)
    > > [...]
    > > end

    >
    > What do you expect
    >
    > get_element(1,2,3)
    >
    > to do?


    auid =3D 1
    document =3D 2
    selector =3D 3
    check_etag =3D true

    Humm, I understand now... Having it, the second parameter "document=3Dnil" =
    is=20
    useless at all since it requires being declared anyway.

    Ok, I've solved my problem by leaving the method as follows:

    def get_element(auid, document, selector, check_etag=3Dtrue)


    Thanks a lot.


    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Jul 21, 2009
    #3
  4. Iñaki Baz Castillo wrote:
    > Hi, I expected that the following is valid in Ruby 1.9:
    >
    > def get_element(auid, document=nil, selector, check_etag=true)
    > [...]
    > end
    >
    > However I get an error:
    >
    > syntax error, unexpected '=', expecting ')' (SyntaxError) ...,
    > element_selector, check_etag=true)
    > ^
    >
    > Thanks for any tip.


    You couldn't you rearrange it to?

    def get_element(auid, selector, document=nil, check_etag=true)
    [...]
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Joseph Lenton, Jul 21, 2009
    #4
  5. El Mi=C3=A9rcoles, 22 de Julio de 2009, Joseph Lenton escribi=C3=B3:
    > I=C3=B1aki Baz Castillo wrote:
    > > Hi, I expected that the following is valid in Ruby 1.9:
    > >
    > > def get_element(auid, document=3Dnil, selector, check_etag=3Dtrue)
    > > [...]
    > > end
    > >
    > > However I get an error:
    > >
    > > syntax error, unexpected '=3D', expecting ')' (SyntaxError) ...,
    > > element_selector, check_etag=3Dtrue)
    > > ^
    > >
    > > Thanks for any tip.

    >
    > You couldn't you rearrange it to?
    >
    > def get_element(auid, selector, document=3Dnil, check_etag=3Dtrue)
    > [...]
    > end


    Yes, I could, but I prefer to keep the order.

    =2D-=20
    I=C3=B1aki Baz Castillo <>
     
    Iñaki Baz Castillo, Jul 22, 2009
    #5
  6. Iñaki Baz Castillo wrote:
    > Hi, I expected that the following is valid in Ruby 1.9:
    >
    > def get_element(auid, document=nil, selector, check_etag=true)
    > [...]
    > end
    >
    > However I get an error:
    >
    > syntax error, unexpected '=', expecting ')' (SyntaxError) ...,
    > element_selector, check_etag=true)
    > ^
    >
    > Thanks for any tip.


    def get_element(auid, *rest)
    case rest.size
    ... etc
    end

    Are you saying that get_element(1,2) would use auid=1 and selector=2,
    but get_element(1,2,3) would use auid=1 and document=2 and selector=3?
    If so it might be slightly better documented as

    def get_element(auid, document_or_selector, *rest)
    case rest.size
    when 0
    document = nil
    selector = document_or_selector
    check_etag = true
    when 1
    document = document_or_selector
    selector = rest[0]
    check_etag = true
    when 2
    document = document_or_selector
    selector = rest[0]
    check_etag = rest[1]
    else
    raise ArgumentError, "wrong number of arguments (#{rest.size}+2)"
    end
    ... etc
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jul 22, 2009
    #6
    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.

Share This Page