different order of parameters...

J

JZ

I'm trying to understand how to call methods in Ruby. Is it possible to
call parameters in different order? E.g. in Python it is very clear and
simple to use:

def fun(a=1,b=2):
return 'a:%s, b:%s' % (a,b)

print fun(b=10, a=20) # a:20, b:10
print fun(b=111) # a:1, b:111

But in similiar Ruby function I get weird results :(

def fun(a=1,b=2)
"a:#{a}, b:#{b}"
end

p fun:)b=>10, :a=>20) # "a:a20b10, b:2"
p fun:)b=>111) # "a:b111, b:2"
p fun(b=111) # "a:111, b:2"

Is it possible that such basic and usefull feature is not implemented in
Ruby??
 
Y

Yukihiro Matsumoto

Hi,


In message "Re: different order of parameters..."
|
|I'm trying to understand how to call methods in Ruby. Is it possible to
|call parameters in different order? E.g. in Python it is very clear and
|simple to use.
|
|def fun(a=1,b=2):
| return 'a:%s, b:%s' % (a,b)
|
|print fun(b=10, a=20) # a:20, b:10
|print fun(b=111) # a:1, b:111

|Is it possible that such basic and usefull feature is not implemented in
|Ruby??

No. Keyword arguments like

fun(b:20, a:10)

is planned, but named keyword arguments will be separated from
positional arguments still.

matz.
 
A

Austin Ziegler

In message "Re: different order of parameters..."
|
| I'm trying to understand how to call methods in Ruby. Is it
| possible to call parameters in different order? E.g. in Python it
| is very clear and simple to use.
|=20
| def fun(a=3D1,b=3D2):
| return 'a:%s, b:%s' % (a,b)
|
| print fun(b=3D10, a=3D20) # a:20, b:10
| print fun(b=3D111) # a:1, b:111
|
| Is it possible that such basic and usefull feature is not
| implemented in Ruby??
No. Keyword arguments like
=20
fun(b:20, a:10)
=20
is planned, but named keyword arguments will be separated from
positional arguments still.

Why will they be separated, Matz? Is it because it's easier to
implement, or will it be for some other reason? This is one of the
few choices about Ruby 2.0 that I'm not quite comfortable with.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
Y

Yukihiro Matsumoto

In message "Re: different order of parameters..."

|Why will they be separated, Matz? Is it because it's easier to
|implement, or will it be for some other reason? This is one of the
|few choices about Ruby 2.0 that I'm not quite comfortable with.

Just because I don't prefer rather complex argument application rules
in Python, for example, in [ruby-talk:150408]. But I was thinking to
revisit and settle keyword arguments behavior in Ruby 2.0 in the near
future, so let me consider your opinion as well.

matz.
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top