简体   繁体   中英

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?

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM