'**' as hash splat?

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.

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.

···

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}

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

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}

···

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.

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

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 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

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,

method :

NameError: undefined method `' for class `Object'

Kalman

Ken Bloom 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.

···

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

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

Hi --

···

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,

> 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]

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Maybe you want to try this

Array.instance_method :

Cheers
Robert

···

On 10/24/06, Kalman Noel <invalid@gmx.net> 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,

> method :
NameError: undefined method `' for class `Object'

Kalman

--
The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.

- George Bernard Shaw

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

I want this toy +1

>
> > T.
><SNIP>
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.

Completely agree
Robert

···

On 10/24/06, Trans <transfire@gmail.com> wrote:

--
The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.

- George Bernard Shaw

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

···

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,
>
> > 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]

But I think that Kalman's point wasn't that there are no : methods,
which of course there are, but that the snippet

[3, *a]

Isn't a method call but syntax.

And adding to that, splat isn't just for method parameters, but for
cases like this as well as parallel assignment.

And I think of splat as primarily a parallel assignment 'thing' since
both argument passing and literal construction can be viewed as usages
of parallel assignment.

···

On 10/25/06, Robert Dober <robert.dober@gmail.com> wrote:

On 10/24/06, Kalman Noel <invalid@gmx.net> 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,
>
> > method :
> NameError: undefined method `' for class `Object'

Maybe you want to try this

Array.instance_method :

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Hi --

···

On Wed, 25 Oct 2006, Jacob Fugal wrote:

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,
>
> > 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.

Right, I didn't pick up on that.

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Rick DeNatale:

Robert Dober:

Kalman Noel:

Ken Bloom:

Trans:

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

Since is a method,

> method :
NameError: undefined method `' for class `Object'

Array.instance_method :

But I think that Kalman's point wasn't that there are no : methods,
which of course there are, but that the snippet

[3, *a]

Isn't a method call but syntax.

Exactly.

Kalman

You are right about all you were saying, only I did not want to make any
statements about that,
I just wanted to underline that : was an instance method of Array as I
did not think about Kalman's point .
I thaught about the impact on an interested newbie reader.
Was I too harsh/quick/offensive? If so I am sorry;
I thaught it was an important minor point to make and I thaught I was quite
alligned with Kalman's style but I have no problem in admiting errors :wink:
Cheers
Robert

···

On 10/26/06, Rick DeNatale <rick.denatale@gmail.com> wrote:

On 10/25/06, Robert Dober <robert.dober@gmail.com> wrote:
> On 10/24/06, Kalman Noel <invalid@gmx.net> 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,
> >
> > > method :
> > NameError: undefined method `' for class `Object'

>
> Maybe you want to try this
>
> Array.instance_method :

But I think that Kalman's point wasn't that there are no : methods,
which of course there are, but that the snippet

[3, *a]

Isn't a method call but syntax.

And adding to that, splat isn't just for method parameters, but for
cases like this as well as parallel assignment.

And I think of splat as primarily a parallel assignment 'thing' since
both argument passing and literal construction can be viewed as usages
of parallel assignment.

--

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

--
The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.

- George Bernard Shaw