How to Remove Empty String from Array in Ruby?
Last Updated :
02 Apr, 2024
In this article, we will learn how to remove empty strings from an array in Ruby. We can remove empty strings from an array in Ruby using various methods.
Using Reject
The reject
method creates a new array containing elements for which the block returns false or nil. In this case, we'll use it to reject empty strings.
Syntax:
array.reject { |element| element.empty? }
Example:
In this example, we have an array of strings that includes some empty strings. We use reject
to create a new array that excludes these empty strings.
Ruby
# Original array containing strings,
# including empty strings
array = ["hello", "", "world", "", "ruby"]
# Removing empty strings from an array
# using reject to remove empty strings
result = array.reject { |element| element.empty? }
puts result.inspect
Output["hello", "world", "ruby"]
Using Select
The select
method returns a new array containing elements for which the block returns true. By using select
with the condition element != ""
, we can remove empty string from array.
Syntax:
array.select { |element| element != "" }
Example:
In this example, we use select to create a new array containing elements that are not empty strings.
Ruby
# Original array containing strings,
# including empty strings
array = ["hello", "", "world", "", "ruby"]
#Removing empty strings using select
# to remove empty strings
result = array.select { |element| element != "" }
puts result.inspect
Output["hello", "world", "ruby"]
Using Map and Compact
The compact method removes nil elements from an array. To remove empty strings, we can first use map to convert empty strings to nil and then use compact
Syntax:
array.map { |element| element.empty? ? nil : element }.compact
Example:
In this example, we convert each empty string to nil using map, and then use compact to remove the nil values, effectively removing the empty strings.
Ruby
# Original array containing strings,
# including empty strings
array = ["hello", "", "world", "", "ruby"]
# Removing empty strings using map and
# compact to remove empty strings
result = array.map { |element| element.empty? ? nil : element }.compact
puts result.inspect
Output["hello", "world", "ruby"]
Similar Reads
How to Remove Last Character from String in Ruby? Removing the last character from a string in Ruby is a common task in various programming scenarios. Whether you need to manipulate user input, process file paths, or clean up data, there are several approaches to achieve this. This article focuses on discussing how to remove the last character from
2 min read
How to Remove Special Characters from a String in Ruby? In this article, we will discuss how to remove special characters from a string in Ruby. In Ruby, special characters can be removed from a string using various methods. Special characters typically include punctuation marks, symbols, and non-alphanumeric characters. Let us explore different methods
2 min read
How to remove double quotes from String in Ruby? In this article, we will discuss how to remove double quotes from a string using Ruby. We can remove double quotes from strings through different methods ranging from using gsub Method with Regular Expression to delete Method Table of Content Removing double quotes from string using gsub Method with
2 min read
How to Remove Duplicate Elements from Array in Ruby? This article focuses on discussing how to remove duplicate elements from an array in Ruby. In Ruby, an array is a fundamental data structure that stores a collection of elements in a specific order. Various methods and approaches can be used to remove duplicate elements from an array. Removing Dupli
2 min read
How to remove all nil elements from an Array in Ruby permanently? In this article, we will discuss how to remove all nil elements from an array permanently in Ruby. We can remove all nil elements from an array permanently through different methods ranging from using compact! method to delete method with nil argument. Table of Content Removing all nil elements from
2 min read