R
Roger Pack
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
Thanks!
-=r
should there not be an Array#to_h?
Thanks!
-=r
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
Thanks!
=> {1=>2, 3=>4}class Array
def to_h
Hash[*self]
end
end => nil
[1,2,3,4].to_h
2009/1/31 Joshua Ballanco said:
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
Thanks!
-=r
Hi --
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
Not if it's not useful It might be, though. It's been talked about
a lot over the years. As I recall, part of the problem is the question
of what it would mean; for example, given this:
["a","b","c","d"].to_h
is it
["a" => "b", "c" => "d"]
or
[0 => "a", 1 => "b", ....]
?
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
2009/2/6 Trans said:The variety of possible good definitions make this hard to define. So
it's understandable that is is not in Ruby core, though being able to
convert back and forth between Hash#to_a and Array#to_h makes the most
sense.
I was thinking about it some more and I was thinking about how the
various options might be addressed. So I came up with this:
# Converts an associative array into a hash.
By using a +mode+ we can offer a variety of common means of
conversion.
Of course, we could just make them all separate methods, ie. #to_h,
#to_h_array, #to_h_splat, #to_h_flat. Maybe that is better?
But then
that seems a bit more limiting, less dynamic, less open for new modes
or multiple labels for a single mode, and the method names look funny
This is generally considered better practice over switching behavior
of a method with an argument. =A0Just think about the length of the
method which increases in with the number of different algorithms
(modes). =A0I prefer short methods.
Alternatively make the algorithm detection automatic. Even in that
case I had to_h only do the detection and then delegate to any one of
Not at all: you can simply add another method.
This is how I'd approach it:
module Enumerable
=A0 def to_h
=A0 =A0 pairs =3D arr =3D 0
=A0 =A0 each do |e|
=A0 =A0 =A0 if Array =3D=3D=3D e
=A0 =A0 =A0 =A0 if e.size <=3D 2
=A0 =A0 =A0 =A0 =A0 pairs +=3D 1
=A0 =A0 =A0 =A0 else
=A0 =A0 =A0 =A0 =A0 arr +=3D 1
=A0 =A0 =A0 =A0 end
=A0 =A0 =A0 end
=A0 =A0 end
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
Thanks!
-=r
of what it would mean; for example, given this:
=A0 =A0["a","b","c","d"].to_h
is it
=A0 =A0["a" =3D> "b", "c" =3D> "d"]
True, but adding a new method is a "bigger deal" than just adding
another parameter option.
But I like your idea, I could create the different methods and then
dispatch from to_h, offering the best of both options.
Exactly.
I'm not sure. On one hand I like it, though I am hesitant about it b/c
it means a whole pass over the array upfront, it won't be very fast.
What do you think about the performance characteristics? On the other
hand, it means the one method #to_h will do quite different things
depending on the form of the data structure passed to it. Is that a
good idea?
class Array
def to_h
Hash[*self]
end
end => nil
[1,2,3,4].to_h => {1=>2, 3=>4}
Hi --
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
Not if it's not useful It might be, though. It's been talked about
a lot over the years. As I recall, part of the problem is the question
of what it would mean; for example, given this:
["a","b","c","d"].to_h
is it
["a" => "b", "c" => "d"]
or
[0 => "a", 1 => "b", ....]
?
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!
It's fairly trivial, isn't it?Hi --
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
Not if it's not useful It might be, though. It's been talked about
a lot over the years. As I recall, part of the problem is the question
of what it would mean; for example, given this:
["a","b","c","d"].to_h
is it
["a" => "b", "c" => "d"]
or
[0 => "a", 1 => "b", ....]
?
=> {1=>2, 3=>4}class Array
def to_h
Hash[*self]
end
end => nil
[1,2,3,4].to_h
Hi --
It's fairly trivial, isn't it?Hi --
On Sun, 1 Feb 2009, Roger Pack wrote:
Not that I would find it useful at all, but is there is a Hash#to_a
should there not be an Array#to_h?
Not if it's not useful It might be, though. It's been talked
about
a lot over the years. As I recall, part of the problem is the
question
of what it would mean; for example, given this:
["a","b","c","d"].to_h
is it
["a" => "b", "c" => "d"]
or
[0 => "a", 1 => "b", ....]
?
=> {1=>2, 3=>4}class Array
def to_h
Hash[*self]
end
end => nil
[1,2,3,4].to_h
Yes, if that's the semantics you want. If you're inclined toward
thinking it should use the array indices as hash keys and the array
values as hash values, then that implementation wouldn't work. My
point was that there's been debate about the semantics -- i.e., what
Array#to_h should actually do.
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!
* Robert Klemme said:Trust me, parameters that change a methods behavior are inferior to
having separate methods.
thinking it should use the array indices as hash keys and the array
values as hash values, then that implementation wouldn't work.
=> {0=>[1, 2], 1=>[3, 4]}Hash[(0..(a.size - 1)).zip(a)]
Yes, if that's the semantics you want. If you're inclined toward
thinking it should use the array indices as hash keys and the array
values as hash values, then that implementation wouldn't work.
With that constructor at hand, it wouldn't be too difficult to get
that behaviour though:
a = [[1,2],[3,4]]=> {0=>[1, 2], 1=>[3, 4]}Hash[(0..(a.size - 1)).zip(a)]
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.