Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

I want to achieve this:

["notification_email"]

>Solution :

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

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

["notification_email"]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading