[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

simple question re Hash and arrays

Esmail

3/2/2008 11:46:00 AM

Hello all,

How come Hash[1,2,3,4] works, but

a=[1,2,3,4]

Hash[a] doesn't?
3 Answers

Esmail

3/2/2008 11:58:00 AM

0

Esmail wrote:
> Hello all,
>
> How come Hash[1,2,3,4] works, but
>
> a=[1,2,3,4]
>
> Hash[a] doesn't?

but Hash[*a] does?

a is an array, what is *a ??

Robert Klemme

3/2/2008 12:14:00 PM

0

On 02.03.2008 12:58, Esmail wrote:
> Esmail wrote:
>> Hello all,
>>
>> How come Hash[1,2,3,4] works, but
>>
>> a=[1,2,3,4]
>>
>> Hash[a] doesn't?
>
> but Hash[*a] does?
>
> a is an array, what is *a ??

It's also an Array. "*" is the "spash operator" that will expand an
Array to the argument list.

irb(main):001:0> def f(*a) p a end
=> nil
irb(main):002:0> f(1,2,3)
[1, 2, 3]
=> nil
irb(main):003:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):004:0> f a
[[1, 2, 3]]
=> nil
irb(main):005:0> f a,33
[[1, 2, 3], 33]
=> nil
irb(main):006:0> f *a
[1, 2, 3]
=> nil
irb(main):007:0>

Cheers

robert

Esmail

3/2/2008 1:17:00 PM

0

Robert Klemme wrote:
>
>> a is an array, what is *a ??
>
> It's also an Array. "*" is the "spash operator" that will expand an
> Array to the argument list.

great, thanks Robert, I got it. I really like Ruby, but months
will pass between my using it, so it's like re-learning it every
time ....