Skip to main content

Command Palette

Search for a command to run...

Membership in a list (Set)

Published
1 min read

Using Set is probably the most convenient way to check for membership in a list since values are stored like in a Hash.

Set uses the === method to check membership, which allows:

case 'apple'
when Set['bean', 'cauliflower', 'carrot']
  "vegetable"
when Set['mandarin', 'apple', 'pear']
  "fruit"
end
# => "fruit"

You can also check membership directly:

'apple' === Set['mandarin', 'apple', 'pear']
# => true

'apple' === Set['bean', 'cauliflower', 'carrot']
# => false