Removing square brackets from hash array for value

985 views ruby

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


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 { |hash| hash.transform_values(&:first) }
# [{"name"=>"xx", "status"=>"true"}, {"name"=>"yy", "status"=>"true"}]

posted this

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?


Please login first before posting an answer.