[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array#to_hash

Trans

2/11/2009 3:19:00 PM

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.

8 Answers

Robert Klemme

2/11/2009 9:08:00 PM

0

On 11.02.2009 16:19, Trans wrote:
> 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

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

William James

2/12/2009 12:38:00 AM

0

Trans wrote:

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

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}

David A. Black

2/12/2009 1:28:00 AM

0

Hi --

On Thu, 12 Feb 2009, William James wrote:

> Trans wrote:
>
>> 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.
>
> 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}

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

William James

2/12/2009 7:05:00 AM

0

Trans wrote:

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

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!

Trans

2/13/2009 12:09:00 AM

0



On Feb 12, 2:08=A0am, "William James" <w_a_x_...@yahoo.com> wrote:

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

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

In this, you might want to do something like:

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

T.

ThoML

2/13/2009 4:55:00 AM

0

> In this, you might want to do something like:
>
> =A0 a =3D [:a,:b,:c]
> =A0 h =3D a.to_hash
> =A0 h[0.5] =3D :x
> =A0 h[1.5] =3D :y
> =A0 h[2.5] =3D :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?

Trans

2/13/2009 7:28:00 PM

0



On Feb 12, 11:54=A0pm, Tom Link <micat...@gmail.com> wrote:
> > In this, you might want to do something like:
>
> > =A0 a =3D [:a,:b,:c]
> > =A0 h =3D a.to_hash
> > =A0 h[0.5] =3D :x
> > =A0 h[1.5] =3D :y
> > =A0 h[2.5] =3D :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?

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.

Nemesis Fixx

2/16/2009 1:53:00 PM

0

yes i agree with you Trans
lots more has no prob.