Remove array within hash

6 views
Skip to first unread message

Manivannan Jeganathan

unread,
Sep 2, 2010, 6:49:33 AM9/2/10
to rubyonra...@googlegroups.com
My orginal hash is like as

==> hash = {"sku_id"=>[4], "brand_active"=>["true"],
"salesman_active"=>["true"]}

How to remove the array within hash. that means to convert the hash like

==> {"sku_id"=>4, "brand_active"=>"true", "salesman_active"=>"true"
--
Posted via http://www.ruby-forum.com/.

Aleksey Gureiev

unread,
Sep 2, 2010, 6:57:47 AM9/2/10
to rubyonra...@googlegroups.com
Come on, why even ask this!? Check the doc for all essentials -- Array, Hash, String.

hash.each { |k,v| hash[k] = v[0] }

- A

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>

PGP.sig

Dave Aronson

unread,
Sep 7, 2010, 1:30:44 PM9/7/10
to Ruby on Rails: Talk
On Sep 2, 6:49 am, Manivannan Jeganathan <li...@ruby-forum.com> wrote:

> ==> hash = {"sku_id"=>[4], "brand_active"=>["true"],
> "salesman_active"=>["true"]}
>
> How to remove the array within hash.

In addition to Aleksey's response re how to UNdo the arraying, would
it be possible simply to NOT put the data in as arrays in the first
place?

-Dave
Reply all
Reply to author
Forward
0 new messages