Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Keyword parameters
Glenn
6/11/2008 7:22:00 PM
[Note: parts of this message were removed to make it a legal post.]
Hi everyone,
I have a question about keyword parameters in Ruby. I know that v1.8.6 doesn't do this and v1.9 does, and that you can 'trick' v1.8.6 into doing it with hashes.
My question is whether you can do this code:
def aProc(options = {})
a = options[:a] || 5
b = options[:b] || 6
c = options[:c] || 7
print a, b, c
end
in such a way where you wouldn't have to explicitly say that the local variable a is equal to the value in the hash with symbol :a? In other words, to do something like iterate through the hash and turn all of the symbol keys into local variable with their respective values?
Thanks,
Glenn
----- Original Message ----
From: Jason Roelofs <jameskilton@gmail.com>
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Monday, March 3, 2008 4:36:49 PM
Subject: Re: Why is this not implemented in Ruby
Show me a language that does allow you to do this, I've never seen it.
Even then, Ruby doesn't deal with method overloads via parameter
lists, so there's a fundamental reason why it won't work:
def foo(a)
end
def foo(a, b)
end
def foo(a, b, c)
end
foo(1) # => ArgumentError: wrong number of arguments (1 for 3)
Ruby 1.9 has keyword arguments, so you'll be able to get past that
easily then. You can fake it in 1.8 with a hash:
def aProc(options = {})
a = options[:a] || 5
b = options[:b] || 6
c = options[:c] || 7
print a, b, c
end
aProc(:a => 1, :c => 3)
With Ruby 1.9 that will look like (I think, doing this from memory):
aProc(a: 1, c: 3)
Jason
On Mon, Mar 3, 2008 at 4:27 PM, Damjan Rems <d_rems@yahoo.com> wrote:
>
> It is probably a good reason but why is this not implemented in ruby.
>
> irb(main):005:0> def aProc(a=5,b=6,c=7)
> irb(main):006:1> print a,b,c
> irb(main):007:1> end
> irb(main):008:0> aProc
> 567=> nil
>
> irb(main):009:0> aProc(1,,3)
> SyntaxError: compile error
> (irb):9: syntax error, unexpected ','
> aProc(1,,3)
> ^
> from (irb):9
> from :0
>
> Why oh why I can not omit parameter in the middle of statement? I am
> forced to know its default value if I want to omit a parameter in the
> middle or at the begining of the statement.
>
>
> by
> TheR
> --
> Posted via
http://www.ruby-...
.
>
>
2 Answers
Mikael Høilund
6/11/2008 10:28:00 PM
0
On Jun 11, 2008, at 21:21, Glenn wrote:
> [T]o do something like iterate through the hash and turn all of the =20=
> symbol keys into local variable with their respective values?
No.
What you *can* do, however, is to simply refer to each of these =20
options directly in the hash, making the hash values default instead. =20=
The na=EFve way:
options[:a] ||=3D 5
...
However, a more preferable way, however, is defining a hash with the =20
default, and then letting the options hash override these:
options =3D {:a =3D> 5, ...}.merge(options)
You can then access each option as option[:a] .... There are a few =20
caveat with this, but these aren't usually a problem.
--=20
Mikael H=F8ilund
http://ho...
fedzor
6/11/2008 11:03:00 PM
0
What makes an *actual* keyword argument, and why don't symbols suffice?
---------------------------------------------------------------|
~Ari
"I don't suffer from insanity. I enjoy every minute of it" --1337est
man alive
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Keyword parameters
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password