how to find that array include only some multiple values ? ruby

17 views
Skip to first unread message

Abdel Latif

unread,
Feb 26, 2018, 6:29:06 AM2/26/18
to Ruby on Rails: Talk
Hi,

I would like to find a way to tell me if an array has only some items but not others, for example :

employment_status = ['Hired','Hired','Provisionally Hired','Hired','Hired','Provisionally Hired','Hired','Provisionally Hired','Hired']

I want to test this array to see if it contains only :
Hired
or
Provisionally Hired
or both

but not any other string.

Thanks,
your help is appreciated.



j...@room118solutions.com

unread,
Feb 26, 2018, 1:40:51 PM2/26/18
to Ruby on Rails: Talk
There are a few ways you could do this, but if you already have the array you want to check, and an array of values that you are looking for, you could do something like this:

employment_status = ['Hired','Hired','Provisionally Hired','Hired','Hired','Provisionally Hired','Hired','Provisionally Hired','Hired']

if (employment_status - ['Hired', 'Provisionally Hired']).empty?
 
# ...
end

That removes the values you are looking for from the original array, and if it's empty, means that the array was comprised entirely of those values.

Jim

Colin Law

unread,
Feb 26, 2018, 4:35:31 PM2/26/18
to Ruby on Rails: Talk
On 21 February 2018 at 19:35, Abdel Latif <mlotf...@gmail.com> wrote:
> Hi,
>
> I would like to find a way to tell me if an array has only some items but
> not others, for example :
>
> employment_status = ['Hired','Hired','Provisionally
> Hired','Hired','Hired','Provisionally Hired','Hired','Provisionally
> Hired','Hired']
>
> I want to test this array to see if it contains only :
> Hired
> or
> Provisionally Hired
> or both

Array.include? might be what you are looking for.

Colin

>
> but not any other string.
>
> Thanks,
> your help is appreciated.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/3e342669-ada4-4b38-b47a-56ff58912e6d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages