Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Default parameter values for methods in Ruby 1.9

Reply
Thread Tools

Default parameter values for methods in Ruby 1.9

 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      07-21-2009
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 <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      07-21-2009
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

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      07-21-2009
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 <(E-Mail Removed)>

 
Reply With Quote
 
Joseph Lenton
Guest
Posts: n/a
 
      07-21-2009
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/.

 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      07-21-2009
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 <(E-Mail Removed)>

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      07-22-2009
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parameter Type/Size Property Values for VARCHAR(MAX) Stored Proc Parameter jcpc ASP .Net 2 01-26-2011 11:48 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
is it possible to get hidden parameter values using window.opener.document.form.parameter.value. Abdul Mohsin Javascript 1 09-06-2005 03:38 PM
Re: Procedure declarations: parameter lists with default values deep VHDL 0 04-15-2004 08:06 AM



Advertisments