[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

'**' as hash splat?

Trans

10/24/2006 3:50:00 AM

We can:

a = [2,1]
[3,*a] #=> [3,2,1]

How about:

h = {:b=>2, :a=>1}
{:c => 3, **h} #=> {:c=>3, :b=>2, :a=>1}

T.

12 Answers

matt

10/24/2006 4:53:00 AM

0

Trans <transfire@gmail.com> wrote:

> We can:
>
> a = [2,1]
> [3,*a] #=> [3,2,1]
>
> How about:
>
> h = {:b=>2, :a=>1}
> {:c => 3, **h} #=> {:c=>3, :b=>2, :a=>1}

How about:

class Hash; alias_method :<<, :merge!; end

So, for example:

h = {:b=>2, :a=>1}
{:c => 3} << h
#=> {:c=>3, :b=>2, :a=>1}

But perhaps I'm missing some desideratum other than brevity. m.
--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Ken Bloom

10/24/2006 1:28:00 PM

0

On Mon, 23 Oct 2006 20:49:55 -0700, Trans wrote:

> We can:
>
> a = [2,1]
> [3,*a] #=> [3,2,1]
>
> How about:
>
> h = {:b=>2, :a=>1}
> {:c => 3, **h} #=> {:c=>3, :b=>2, :a=>1}
>
> T.

The purpose of splat is to convert an array into a list of parameters to a
method. Since [] is a method, this happens to work well for including an
array into another array. But that's not its purpose, just a side effect.

There's no real concept of converting a hash into a list of parameters to
a function, so there's no splat notation for it.

If you want to combine hashes, use the merge method

{:c=>3}.merge(h) #=> {:a=>1, :b=>2, :c=>3}

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...
I've added a signing subkey to my GPG key. Please update your keyring.

Kalman Noel

10/24/2006 2:43:00 PM

0

Ken Bloom:
> On Mon, 23 Oct 2006 20:49:55 -0700, Trans wrote:
>> a = [2,1]
>> [3,*a] #=> [3,2,1]
> The purpose of splat is to convert an array into a list of parameters to a
> method. Since [] is a method,

irb> method :[]
NameError: undefined method `[]' for class `Object'

Kalman

dblack

10/24/2006 2:53:00 PM

0

Jacob Fugal

10/24/2006 3:12:00 PM

0

On 10/24/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> On Tue, 24 Oct 2006, Kalman Noel wrote:
> > Ken Bloom:
> >> On Mon, 23 Oct 2006 20:49:55 -0700, Trans wrote:
> >>> a = [2,1]
> >>> [3,*a] #=> [3,2,1]
> >> The purpose of splat is to convert an array into a list of parameters to a
> >> method. Since [] is a method,
> >
> > irb> method :[]
> > NameError: undefined method `[]' for class `Object'
>
> irb(main):003:0> a = [2,1]
> => [2, 1]
> irb(main):004:0> [3, *a]
> => [3, 2, 1]

I think Kalman was just pointing out that [] (in this case as the
array literal syntax) is *not* a method, contrary to what Ken had
claimed.

Jacob Fugal

dblack

10/24/2006 3:42:00 PM

0

Rolf Gebauer

10/24/2006 4:01:00 PM

0


Trans schrieb:

> We can:
>
> a = [2,1]
> [3,*a] #=> [3,2,1]
>
> How about:
>
> h = {:b=>2, :a=>1}
> {:c => 3, **h} #=> {:c=>3, :b=>2, :a=>1}
>
> T.
we can do by using method Hash::[] instead of literal {}

Hash[ :c, 3, *h ] # => {:c=>3, [:a, 1]=>[:b, 2]}


Rolf

Rolf Gebauer

10/24/2006 4:01:00 PM

0


Trans schrieb:

> We can:
>
> a = [2,1]
> [3,*a] #=> [3,2,1]
>
> How about:
>
> h = {:b=>2, :a=>1}
> {:c => 3, **h} #=> {:c=>3, :b=>2, :a=>1}
>
> T.
we can do by using method Hash::[] instead of literal {}

Hash[ :c, 3, *h ] # => {:c=>3, [:a, 1]=>[:b, 2]}


Rolf

Trans

10/24/2006 4:06:00 PM

0


Ken Bloom wrote:
> On Mon, 23 Oct 2006 20:49:55 -0700, Trans wrote:
>
> > We can:
> >
> > a = [2,1]
> > [3,*a] #=> [3,2,1]
> >
> > How about:
> >
> > h = {:b=>2, :a=>1}
> > {:c => 3, **h} #=> {:c=>3, :b=>2, :a=>1}
> >
> > T.
>
> The purpose of splat is to convert an array into a list of parameters to a
> method. Since [] is a method, this happens to work well for including an
> array into another array. But that's not its purpose, just a side effect.
>
> There's no real concept of converting a hash into a list of parameters to
> a function, so there's no splat notation for it.

Not so for Ruby 2.0, assuming we do indeed get key parameters:

def foo( **keys )
p keys
end

> If you want to combine hashes, use the merge method
>
> {:c=>3}.merge(h) #=> {:a=>1, :b=>2, :c=>3}

And with arrays one can use #+. That's fine, but it lacks a certain
elegance in some instances.
T.

Rolf Gebauer

10/25/2006 9:44:00 AM

0


Rolf Gebauer schrieb:

> Trans schrieb:
>
> > We can:
> >
> > a = [2,1]
> > [3,*a] #=> [3,2,1]
> >
> > How about:
> >
> > h = {:b=>2, :a=>1}
> > {:c => 3, **h} #=> {:c=>3, :b=>2, :a=>1}
> >
> > T.
> we can do by using method Hash::[] instead of literal {}
>
> Hash[ :c, 3, *h ] # => {:c=>3, [:a, 1]=>[:b, 2]}
>
>

sorry for (twice) rubbish
must be :
Hash[ :c, 3, *h.to_a.flatten ] # => {:c=>3, [:a, 1]=>[:b, 2]}

Rolf