w3resource

Ruby Array Exercises: Check whether a value exists in an array

Ruby Array: Exercise-1 with Solution

Write a Ruby program to check whether a value exists in an array.

Ruby Array Exercises: Check whether a value exists in an array

Ruby Code:

color = ["Red", "Green", "Blue", "White"]
print "Original array:\n"
print color
print "\nCheck if 'Green' in color array!\n"
print color.include? 'Green'
print "\nCheck if 'Pink' in color array!\n"
print color.include? 'Pink'

Output:

Original array:
["Red", "Green", "Blue", "White"]
Check if 'Green' in color array!
true
Check if 'Pink' in color array!
false

Flowchart:

Flowchart: Check whether a value exists in an array

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Ruby Basic Exercises
Next: Write a Ruby program to check whether 7 appears as either the first or last element in an given array. The array length must be 1 or more.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/ruby-exercises/array/ruby-array-exercise-1.php