Gareth Burrows
Gareth Burrows

Reputation: 1182

ruby search within an array

I have an array os users called, strangely enough, @users.

Is it possible to search WITHIN this array to further narrow down the results. What I am trying to do is the following

@users.where(:gender => nil)

and end up with a smaller array of users so I can report missing data. is this possible?

Upvotes: 0

Views: 71

Answers (1)

Jaffa
Jaffa

Reputation: 12700

@users.select{|x| x.gender.nil?}

Or do the inverse (if you have no falsey gender)

@users.reject(&:gender)

If @users is a collection of objects you retrieve from a database, you most certainly can do something like:

@users.pluck(:gender)

This get all the non-nil values for most database adapters.

Upvotes: 8

Related Questions