Robert Klemme
1/25/2005 8:10:00 AM
"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0501241746590.31348@wobblini...
> Hi --
>
> On Tue, 25 Jan 2005, Michael Neumann wrote:
>
> > Eustaquio Rangel de Oliveira Jr. wrote:
> >> -----BEGIN PGP SIGNED MESSAGE-----
> >> Hash: SHA1
> >>
> >> Hi!
> >>
> >> If I have:
> >>
> >> h = {"a"=>"111","b"=>"222"}
> >> h.to_a
> >> => [["a", "111"], ["b", "222"]]
> >>
> >> is there a method to convert a Hash into a "plain" Array like
> >
> > h.to_a.flatten will do the trick.
>
> However, if any of the hash values are arrays, they'll get flattened
> too:
>
> { "a" => [1,2,3] }.to_a.flatten # => ["a",1,2,3]
>
> unless of course you do:
>
> require 'flattenx' # from RAA
> { "a" => [1,2,3] }.to_a.flatten_by(1) # => ["a",[1,2,3]]
>
> :-)
.... or use #inject which also has the advantage of saving the intermediate
array which is created by Hash#to_a:
>> h = {"a"=>"111","b"=>"222"}
=> {"a"=>"111", "b"=>"222"}
>> h.inject([]){|a,(k,v)| a << k << v}
=> ["a", "111", "b", "222"]
Btw, did I mention that I love #inject? :-)
Kind regards
robert