Forum: Ruby Array#to_hash

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.
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2009-02-11 16:22
(Received via mailing list)
Would any problems arise from extending Array with this?

  # Convert an array into an indexed hash.
  #
  # [:a,:b,:c].to_hash  #=> {0=>:a,1=>:b,2=>:c}
  #
  def to_hash
    h = {}
    each_with_index do |v, i|
      h[i] = v
    end
    h
  end

Thanks,
T.
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2009-02-11 16:28
(Received via mailing list)
Hi,

In message "Re: Array#to_hash"
    on Thu, 12 Feb 2009 00:19:10 +0900, Trans <transfire@gmail.com>
writes:

|Would any problems arise from extending Array with this?

Nothing I can think of, except that I have never wanted such
conversion from array to hash in my 16 year Ruby programming. ;-)

              matz.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-02-11 21:24
(Received via mailing list)
On Wed, Feb 11, 2009 at 4:26 PM, Yukihiro Matsumoto <matz@ruby-lang.org>
wrote:
> Hi,
>
> In message "Re: Array#to_hash"
>    on Thu, 12 Feb 2009 00:19:10 +0900, Trans <transfire@gmail.com> writes:
>
> |Would any problems arise from extending Array with this?
>
> Nothing I can think of, except that I have never wanted such
> conversion from array to hash in my 16 year Ruby programming. ;-)
>
Yeah the usecase somehow eludes me too. Did you mean
{:a => 0, :b => 1, :c =>2 }
by any chance?

Robert
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-11 22:13
(Received via mailing list)
On 11.02.2009 16:19, Trans wrote:
>     end
>     h
>   end

It is still the question that David has mentioned a few days ago: there
are many reasonable ways for Array to Hash conversion and who decides
which is the "standard" one which should go into Array#to_hash?

Cheers

  robert
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2009-02-12 00:15
(Received via mailing list)
On Feb 11, 10:26 am, Yukihiro Matsumoto <m...@ruby-lang.org> wrote:
> Hi,
>
> In message "Re: Array#to_hash"
>     on Thu, 12 Feb 2009 00:19:10 +0900, Trans <transf...@gmail.com> writes:
>
> |Would any problems arise from extending Array with this?
>
> Nothing I can think of, except that I have never wanted such
> conversion from array to hash in my 16 year Ruby programming. ;-)

True enough. And maybe there never will be. There are many words in
the dictionary I never use too. Still, they have definitions, and if
one were to define Array#to_hash, that would seem like the clear
meaning. (I use #to_h for the more useful conversions).

Thanks,
T.
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2009-02-12 01:40
(Received via mailing list)
Trans wrote:

>     end
>     h
>   end
>
> Thanks,
> T.

Always remember:

"We don't need no stinkin' loops!"

a=[:a,:b,:c]
    ==>[:a, :b, :c]
Hash[ *(0...a.size).zip(a).flatten ]
    ==>{0=>:a, 1=>:b, 2=>:c}
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-12 02:30
(Received via mailing list)
Hi --

On Thu, 12 Feb 2009, William James wrote:

>>     each_with_index do |v, i|
> "We don't need no stinkin' loops!"
>
> a=[:a,:b,:c]
>    ==>[:a, :b, :c]
> Hash[ *(0...a.size).zip(a).flatten ]
>    ==>{0=>:a, 1=>:b, 2=>:c}

You'd want to use flatten(1) so as to avoid over-flattening.
(Available as the flatten_x extension for 1.8.6.)


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2009-02-12 08:11
(Received via mailing list)
Trans wrote:

>     end
>     h
>   end
>
> Thanks,
> T.

a = [:a,:b,:c]
    ==>[:a, :b, :c]
h = a.to_hash
    ==>{0=>:a, 1=>:b, 2=>:c}
a[2]
    ==>:c
h[2]
    ==>:c

Wow!  That *is* a big improvement!
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2009-02-13 01:14
(Received via mailing list)
On Feb 12, 2:08 am, "William James" <w_a_x_...@yahoo.com> wrote:

> a = [:a,:b,:c]
>     ==>[:a, :b, :c]
> h = a.to_hash
>     ==>{0=>:a, 1=>:b, 2=>:c}
> a[2]
>     ==>:c
> h[2]
>     ==>:c
>
> Wow!  That *is* a big improvement!

Ha ha. Very funny. It may seem silly,  but "identity principles" often
do. You know like, a + 0 = a. How useful does that seem? But it is.

In this, you might want to do something like:

  a = [:a,:b,:c]
  h = a.to_hash
  h[0.5] = :x
  h[1.5] = :y
  h[2.5] = :z
  # ... etc ...
  a = h.sort.map{|k,v|v}

T.
9b905791cbdbb1af35b65e02c3217e23?d=identicon&s=25 Tom Link (Guest)
on 2009-02-13 05:56
(Received via mailing list)
> In this, you might want to do something like:
>
>   a = [:a,:b,:c]
>   h = a.to_hash
>   h[0.5] = :x
>   h[1.5] = :y
>   h[2.5] = :z

How often did you actually want to do this during the past five years?
Since you can quite easily convert an array into a hash the way you
want it (by the use of #zip etc.), does this justify an extra method
in Array or maybe even Enumerable?
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2009-02-13 20:30
(Received via mailing list)
On Feb 12, 11:54 pm, Tom Link <micat...@gmail.com> wrote:
> want it (by the use of #zip etc.), does this justify an extra method
> in Array or maybe even Enumerable?

Just to be clear, I'm not asking that it be put into core Ruby. Just
asking if it is a safe extension.

Nonetheless, I have a bit of a different theory about methods in a
language. I think it's good to have a lot of them. Just so long as the
name makes it very clear what they do. I understand there are
performance limits to this, so their are reasons to limit the number.
Also, there is unfortunately too much to learn in the industry these
days, so there's another reason, though a sorry one. Ultimately
computer languages need to become more like verbal ones, and verbal
languages have lots of words, 1,000s are used commonly. 10,000s
irregularly but are used. And 100,000s are available in total to
choose from. I don't think Ruby is going to implode b/c it includes a
few methods that are hardly ever used.

T.
1d02bfabb5427faddb5ecf8ac58ea0bd?d=identicon&s=25 Nemesis Fixx (Guest)
on 2009-02-16 14:53
(Received via mailing list)
yes i agree with you Trans
lots more has no prob.
This topic is locked and can not be replied to.