# Way to combing Hash Definition sans =\> with %w()?

**URL:** <https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099>\
**Category:** Ruby\
**Created:** [December 13, 2005, 12:26am UTC](https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099 "2005-12-13T00:26:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandiebolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandiebolt](https://www.ruby-forum.com/u/dandiebolt)\
**Post date:** [December 13, 2005, 12:26am UTC](https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099/1 "2005-12-13T00:26:21Z")

</div>

I have a large amount of text pairs (no spaces inthe text) that I need  
to turn into a hash. Is there a shorcut that will allow me to create the  
hash without entering the quotes and arrows?

%w(apple bananna orange grape)  
=\> [“apple”, “bananna”, “orange”, “grape”]

```
Hash["apple","bananna","orange","grape"]

```

=\>{“apple”=\>“bananna”, “orange”=\>“grape”}

Thanks!

---

<div class="post-metadata">

**Author:** ![dandiebolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandiebolt](https://www.ruby-forum.com/u/dandiebolt)\
**Post date:** [December 13, 2005, 12:35am UTC](https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099/2 "2005-12-13T00:35:21Z")

</div>

On Dec 12, 2005, at 5:25 PM, Dan D. wrote:

> I have a large amount of text pairs (no spaces inthe text) that I  
> need to turn into a hash. Is there a shorcut that will allow me to  
> create the hash without entering the quotes and arrows?
> 
> %w(apple bananna orange grape)  
> =\> [“apple”, “bananna”, “orange”, “grape”]
> 
> ```
> Hash["apple","bananna","orange","grape"]
> 
> ```
> 
> =\>{“apple”=\>“bananna”, “orange”=\>“grape”}

> > Hash[\*%w(apple bananna orange grape)]  
> > =\> {“apple”=\>“bananna”, “orange”=\>“grape”}

James Edward G. II

---

<div class="post-metadata">

**Author:** ![dandiebolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandiebolt](https://www.ruby-forum.com/u/dandiebolt)\
**Post date:** [December 13, 2005, 12:44am UTC](https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099/3 "2005-12-13T00:44:27Z")

</div>

> Hash[\*%w(apple bananna orange grape)]

Thanks but what exactly is the asterisk doing (what’s the receiver)? I  
started looking for a to\_hash method and found the proposal rejected  
earlier:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/162435](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/162435)

---

<div class="post-metadata">

**Author:** ![dandiebolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandiebolt](https://www.ruby-forum.com/u/dandiebolt)\
**Post date:** [December 13, 2005, 12:59am UTC](https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099/4 "2005-12-13T00:59:30Z")

</div>

On Dec 12, 2005, at 5:41 PM, Dan D. wrote:

> > Hash[\*%w(apple bananna orange grape)]
> 
> Thanks but what exactly is the asterisk doing (what’s the receiver)?

That asterisk is Ruby “splat” or “explode” operator, in this  
context. The receiver is the Array, which is expanded back out into  
its individual elements (can only be used as the final parameter in a  
method call).

It also works in reverse as the “slurp” operator:

def my\_method( arg1, arg2, \*arr\_of\_all\_leftover\_args )

In this case, it slurps the passed args into an Array. Again it must  
be the final parameter (except for a block).

> > def show( one, two, three )  
> > p one  
> > p two  
> > p three  
> > end  
> > =\> nil
> 
> > arr = (1…3).to\_a  
> > =\> [1, 2, 3]
> 
> > show(\*arr)  
> > 1  
> > 2  
> > 3  
> > =\> nil
> 
> > def slurp( \*args )  
> > p args  
> > end  
> > =\> nil
> 
> > slurp(1, 2, 3)  
> > [1, 2, 3]  
> > =\> nil

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![dandiebolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandiebolt](https://www.ruby-forum.com/u/dandiebolt)\
**Post date:** [December 13, 2005, 1:14am UTC](https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099/5 "2005-12-13T01:14:33Z")

</div>

Was this question a ringer or what? Seriously, I love how easy Ruby  
makes some things. Come on Matz, fess up - this was a plant!

🙂

---

<div class="post-metadata">

**Author:** ![dandiebolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandiebolt](https://www.ruby-forum.com/u/dandiebolt)\
**Post date:** [December 13, 2005, 1:38am UTC](https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099/6 "2005-12-13T01:38:41Z")

</div>

On Dec 12, 2005, at 6:29 PM, Dan D. wrote:

> > … in this context.
> 
> Very clever. The hash’s brackets disguise the context. I tried  
> this context but no joy:
> 
> irb\> \*[“apple”, “bananna”, “orange”, "grape]

As I said, it must be the last parameter of a method call. Hash[…]  
is a method call in disguise. 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![dandiebolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandiebolt](https://www.ruby-forum.com/u/dandiebolt)\
**Post date:** [December 13, 2005, 5:54am UTC](https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099/7 "2005-12-13T05:54:13Z")

</div>

Dan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –0-185724427-1134433774=:47703  
> Content-Type: text/plain; charset=iso-8859-1  
> Content-Transfer-Encoding: quoted-printable
> 
> > … in this context.=20  
> > =20  
> > Very clever. The hash’s brackets disguise the context. I tried this con=  
> > text but no joy:=20  
> > =20  
> > irb\> \*[“apple”, “bananna”, “orange”, "grape]

The \* converts an array to a comma separated list (what David Black  
dubbed the “unary unarray operator”). Therefore, it needs to be used in  
a context where a comma separated list of the array’s contents would  
make syntactic sense. Also, for some reason (anyone know why?) it can’t  
be  
inserted anywhere other than at the end of an existing list, so that  
e.g.

a, b, c, d = 1, 2, \*[3,4] # works

a, b, c, d = 1, \*[2,3], 4 # syntax error

martin

---

<div class="post-metadata">

**Author:** ![dandiebolt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandiebolt](https://www.ruby-forum.com/u/dandiebolt)\
**Post date:** [December 13, 2005, 1:32am UTC](https://www.ruby-forum.com/t/way-to-combing-hash-definition-sans-with-w/50099/8 "2005-12-13T01:32:38Z")

</div>

> … in this context.

Very clever. The hash’s brackets disguise the context. I tried this  
context but no joy:

irb\> \*[“apple”, “bananna”, “orange”, "grape]

Thanks again
