Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

How to check if a value exists in an array in Ruby

# Ruby
['Cat', 'Dog', 'Bird'].include? 'Dog'
# => true

# Rails ActiveSupport
'Unicorn'.in?(['Cat', 'Dog', 'Bird'])
# => false

# Via case statement
element = 3
array = [1, 2, 3, 4, 5]
case element
when *array 
  puts 'found in array'
else
  puts 'not found in array'
end
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #check #exists #array #Ruby
ADD COMMENT
Topic
Name
7+6 =