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


#1

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!


#2

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


#3

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


#4

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


#5

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!

:slight_smile:


#6

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

James Edward G. II


#7

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


#8

… 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