How to create an array from an object and only keep positive values?

Viewed 33

I am building a rails 5.2 app.

In this app I need to convert an object to an array, but only keep the attributes that got a true value (in this example notification_email)

The object is this:

{"notification_email"=>true, "notification_alert"=>false}

I want to achieve this:

["notification_email"]
2 Answers

x = {"notification_email"=>true, "notification_alert"=>false}

result = x.delete_if {|x,y| y == false}.keys

["notification_email"]

hash = {"notification_email"=>true, "notification_alert"=>false}

if we want to find out data then we can use select

try=> hash.select{|x,y| y == true}.keys

result: ["notification_email"]
Related