w3resource

Ruby Array Exercises: Remove blank elements from a given array

Ruby Array: Exercise-8 with Solution

Write a Ruby program to remove blank elements from a given array.

Ruby Array Exercises: Remove blank elements from a given array

Ruby Code:

color = ["Red", "Green", "", "Blue", "White"]
print "Original array:\n"
print color
print "\nRemove blank element from the above array:\n"
new_color = color.reject { |c| c.empty? }
print new_color 

Output:

Original array:
["Red", "Green", "", "Blue", "White"]
Remove blank element from the above array:
["Red", "Green", "Blue", "White"]

Flowchart:

Flowchart: Remove blank elements from a given array

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to check two given arrays of integers and test if they have the same first element or they have the same last element. Both arrays length must be 1 or more.
Next: Write a Ruby program to compute the sum of all the elements. The array length must be 3 or more.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.