[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Again, Rite explanation needed (keyword args and new hash syntax

T. Onoma

11/18/2003 10:15:00 AM

> Hi gurus and nubys,
>
> Again here to bother you :)

whos the bigger bother? :) and i wouldn't think you could be rude. (well, maybe if you REALLY tried)

> I wonder:
> why we introduce the ':' syntax ?

its an obvious refection of the new hash syntax. in essence he's saying:

def f(a,:b=>2,**kwd) # == def f(a,b:2,**kwd)

> Why can't we call a kwd arg like
> f(1,b=5)
>
> I suppose this is to avoid conflicts with normal assignment..
> but what's wrong with making go away the assignment in method calling?

i dobt that should go away, but to be honest, i am stumped on the sytax too. when i first saw it i thought "why more syntax" the parameters already have names. we only need a way to specify them in the method call, not in the def!

that's were your = doesn't work. you can make an assignment in a method invocation. that's just common place. (perhaps not used a lot, but enough) but adding => or : sugar to calls causes a conflict with the way in which ruby currently handles hash parameters. so somethings got to give, in order to do:

f(1,:b=>5)

> PS
> btw the 'a: c' syntax for hash is really cool.
>
> I always hate to type 'a'=>b or :a=>b.

got a feeling symbols are going to get a lot more use if this happens. not sure how i feel about it. its certainly not bad, but it means even more syntax to understand. "hey nuby, a:4 and :a=>4 mean the same thing."

> Nobody expects a string literal or a regexp literal to work like the
> rest of the code.

in what way?

> But we expect this from Arrays and Hashes (and somewy Ranges).
> This must mean something.

yes, it cetainly does.

> Possibly that I need more coffe in the morning.

i'll drink to that.

thanks for the bother,
-t0

1 Answer

gabriele renzi

11/18/2003 1:31:00 PM

0

il Tue, 18 Nov 2003 19:14:50 +0900, "T. Onoma" <transami@runbox.com>
ha scritto::





>> Nobody expects a string literal or a regexp literal to work like the
>> rest of the code.
>
>in what way?

well, if you write
a=10
b='a'

you expect b to be 97.chr.

If you write b=/a/ you know it is someway translated in a pattern.
But if you write
b=[a]
or b={a=>4}

you expect 'ciao' in the Array or Hash. For sure this is not wrong..
just interesting :)