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.
jim4
February 26, 2018, 6:40pm
2
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
(Colin Law)
February 26, 2018, 9:33pm
3
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