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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2005-12-13 00:26
(Received via mailing list)
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!
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-13 00:35
(Received via mailing list)
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
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2005-12-13 00:44
(Received via mailing list)
>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/...
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-13 00:59
(Received via mailing list)
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
573b9499030e1ccb867ef80f0ff1ac49?d=identicon&s=25 m4dc4p (Guest)
on 2005-12-13 01:14
(Received via mailing list)
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!

:)
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2005-12-13 01:32
(Received via mailing list)
> ... 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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-13 01:38
(Received via mailing list)
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
2cf6d8e639314abd751f83a72e9a2ac5?d=identicon&s=25 Martin DeMello (Guest)
on 2005-12-13 05:54
(Received via mailing list)
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
This topic is locked and can not be replied to.