Answers for "ruby check if element in array"

6

ruby array has element

>> ['Cat', 'Dog', 'Bird'].include? 'Dog'
=> true
Posted by: Guest on April-08-2020
0

ruby does object exist in array

# Element exists in array
object_exists = arr.any? { |elem| elem == desired_value }

# Element with property exists in array
object_exists = arr.any? { |obj| obj.prop == desired_value }

# Type exists in array
object_exists = arr.any?(Integer)

# Element matching regex exists in array
object_exists = arr.any?(/d/)
Posted by: Guest on November-20-2020

Code answers related to "ruby check if element in array"

Browse Popular Code Answers by Language