Default parameter values for methods in Ruby 1.9

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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 <[email protected]>
 
J

Joel VanderWerf

Iñaki Baz Castillo said:
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?
 
I

Iñaki Baz Castillo

El Martes, 21 de Julio de 2009, Joel VanderWerf escribi=C3=B3:
I=C3=B1aki Baz Castillo said:
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 <[email protected]>
 
J

Joseph Lenton

Iñaki Baz Castillo said:
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
 
I

Iñaki Baz Castillo

El Mi=C3=A9rcoles, 22 de Julio de 2009, Joseph Lenton escribi=C3=B3:
I=C3=B1aki Baz Castillo said:
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 <[email protected]>
 
B

Brian Candler

Iñaki Baz Castillo said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top