[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Dan Diebolt

12/12/2005 11:26:00 PM

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!




---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping
8 Answers

James Gray

12/12/2005 11:33:00 PM

0

On Dec 12, 2005, at 5:25 PM, Dan Diebolt 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 Gray II


Dan Diebolt

12/12/2005 11:41:00 PM

0

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




---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping

James Gray

12/12/2005 11:58:00 PM

0

On Dec 12, 2005, at 5:41 PM, Dan Diebolt 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 Gray II


jgbailey

12/13/2005 12:12:00 AM

0

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!

:)

James Edward Gray II wrote:
> On Dec 12, 2005, at 5:25 PM, Dan Diebolt 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 Gray II

Dan Diebolt

12/13/2005 12:30:00 AM

0

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


---------------------------------
Yahoo! Shopping
Find Great Deals on Holiday Gifts at Yahoo! Shopping

James Gray

12/13/2005 12:38:00 AM

0

On Dec 12, 2005, at 6:29 PM, Dan Diebolt 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 Gray II


Martin DeMello

12/13/2005 4:49:00 AM

0

Dan Diebolt <dandiebolt@yahoo.com> 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

Martin DeMello

12/13/2005 4:49:00 AM

0

Dan Diebolt <dandiebolt@yahoo.com> 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