1
{"offer:manage_all"=>["0", "1"], "offer:index"=>["0"], "offer:new"=>["0"], "offer:show"=>["0"], "offer:create"=>["0"], "offer:update"=>["0"], "offer:destroy"=>["0"], "job:manage_all"=>["0", "1"], "job:index"=>["0", "1"], "job:new"=>["0"], "job:create"=>["0"], "job:edit"=>["0"], "job:update"=>["0"], "job:destroy"=>["0"], "user:manage_all"=>["0", "1"], "user:index"=>["0"], "user:new"=>["0"], "user:create"=>["0"], "user:edit"=>["0"], "user:update"=>["0"], "user:destroy"=>["0"], "account_access:manage_all"=>["0", "1"], "role:manage_all"=>["0", "1"], "role:index"=>["0"], "role:new"=>["0"], "role:create"=>["0"], "role:edit"=>["0"], "role:update"=>["0"], "role:destroy"=>["0"], "welcome_package:manage_all"=>["0", "1"]}

I need to convert this hash into a string.

If the key has a value of 1 the key needs to be pushed to a an array.

The above will has will need to equal this because they are the keys with a value that includes 1.

["offer:manage_all", "job:manage_all", "job:index", "account_access:manage_all", "role:manage_all", "welcome_package:manage_all"]

Thank you in advance.

1
  • 2
    In future, it's easier for the reader if you give an example that is as small as possible but still covers all the bases. Also, it's helpful if you assign a variable to each of the example's input objects (e.g., h = { "offer:manage_all"...}) so that readers can refer to the variable (h) in answers and comments without having to define it (and all readers reference the same variable). Commented Sep 13, 2016 at 6:00

2 Answers 2

4

Given the hash:

h = {"cat"=>["0", "1"], "dog"=>["0"], "pig"=>["0", "1"], "owl"=>["0"], "hen"=>["0", "1"] }

I would write

h.keys.select { |k| h[k].include? "1" }
  #=> ["cat", "pig", "hen"] 
Sign up to request clarification or add additional context in comments.

Comments

3

try method select and keys

hash = {"offer:manage_all"=>["0", "1"], "offer:index"=>["0"], "offer:new"=>["0"], "offer:show"=>["0"], "offer:create"=>["0"], "offer:update"=>["0"], "offer:destroy"=>["0"], "job:manage_all"=>["0", "1"], "job:index"=>["0", "1"], "job:new"=>["0"], "job:create"=>["0"], "job:edit"=>["0"], "job:update"=>["0"], "job:destroy"=>["0"], "user:manage_all"=>["0", "1"], "user:index"=>["0"], "user:new"=>["0"], "user:create"=>["0"], "user:edit"=>["0"], "user:update"=>["0"], "user:destroy"=>["0"], "account_access:manage_all"=>["0", "1"], "role:manage_all"=>["0", "1"], "role:index"=>["0"], "role:new"=>["0"], "role:create"=>["0"], "role:edit"=>["0"], "role:update"=>["0"], "role:destroy"=>["0"], "welcome_package:manage_all"=>["0", "1"]}

keys = hash.select{ |key,val| val.include? "1" }.keys

#=> ["offer:manage_all", "job:manage_all", "job:index", "user:manage_all", "account_access:manage_all", "role:manage_all", "welcome_package:manage_all"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.