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
···
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
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
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
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.
···
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.
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
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?
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.
···
On Feb 12, 11:54 pm, Tom Link <micat...@gmail.com> wrote:
> 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?