Ruby if array include?how to check a array with a value if present.

arr = %w(vietnam china japan)
arr.exclude?('usa') -> true
arr.exclude?('china') -> false

Step -1

arr = ['Cat', 'Dog', 'Bird']
hash = arr.map {|x| [x,true]}.to_h
 => {"Cat"=>true, "Dog"=>true, "Bird"=>true}
hash["Dog"]
 => true
hash["Insect"]
 => false
['Cat', 'Dog', 'Bird'].index('Dog')
>> ['Cat', 'Dog', 'Bird'].include? 'Dog'
=> true

https://stackoverflow.com/questions/1986386/how-to-check-if-a-value-exists-in-an-array-in-ruby#:~:text=This%20is%20another%20way%20to,the%20element%20in%20the%20array.&text=This%20returns%20the%20index%20of,contains%20the%20letter%20’o’.&text=index%20still%20iterates%20over%20the,the%20value%20of%20the%20element.

Leave a Comment

Your email address will not be published. Required fields are marked *