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

D

Dan Diebolt

--0-882415816-1134429946=:10511
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

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 ha=
sh without entering the quotes and arrows?
=20
%w(apple bananna orange grape)
=3D> ["apple", "bananna", "orange", "grape"]
=20
Hash["apple","bananna","orange","grape"]
=3D>{"apple"=3D>"bananna", "orange"=3D>"grape"}
=20
Thanks!

=20

=09
 
J

James Edward Gray II

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
 
J

James Edward Gray II

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[1, 2, 3]
=> nil

Hope that helps.

James Edward Gray II
 
M

m4dc4p

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!

:)
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
 
D

Dan Diebolt

--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]
=20
Thanks again

=09
 
J

James Edward Gray II

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

Martin DeMello

Dan Diebolt said:
--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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top