Removing square brackets from hash array for value

985 views ruby
-3

I am having a following hash array

A = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}

I tried following code to remove the square brackets

A.to_s.gsub("\\[|\\]", "")

but its not working.

How I remove the square brackets to get following output

A = [{"name" => "xx", "status" => "true"}, {"name" => "yy", "status" => "true"}

Kindly assist

answered question

2 Answers

13

Since they're strings inside arrays, the [] is the representation Ruby does of it. Try accessing the first element for each key's value in those hashes:

a = [{"name" => ["xx"], "status" => ["true"]}, {"name" => ["yy"], "status" => ["true"]}]
p a.map { |hash| hash.transform_values(&:first) }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]

posted this
4

Use exists:

select m.*,
       (case when exists (select 1 from assignments a where a.wsid = w.wsid and a.adp_id in (103)
             then 1 else 0
        end) as flag
from macs m;

posted this

Have an answer?

JD

Please login first before posting an answer.