How to Remove Empty String from Array in Ruby? Last Updated : 02 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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. Table of Content Remove empty string from array using rejectRemove empty string from array using selectRemove empty string from array using map and compactUsing RejectThe 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 SelectThe 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 CompactThe 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"] Comment More infoAdvertise with us Next Article How to Remove Empty String from Array in Ruby? A am8254s3a Follow Improve Article Tags : 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 Remove array elements in Ruby In this article, we will learn how to remove elements from an array in Ruby. Method #1: Using Index Ruby # Ruby program to remove elements # in array # creating string using [] str = ["GFG", "G4G", "Sudo", "Geeks"] str.delete_at(0) print str Output: ["G4G", "S 1 min read How to convert Array to Hash in Ruby? In this article, we will discuss how to convert an array to a hash in Ruby. Converting an array to a hash can be useful when we have data in an array format and need to organize it into key-value pairs for easier access and manipulation. Table of Content Converting array to hash using Hash::[] const 3 min read JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po 3 min read Ruby | String empty? Method empty? is a String class method in Ruby which is used to check whether the string length is zero or not. Syntax: str.empty? Parameters: Here, str is the given string which is to be checked. Returns: It returns true if str has a length of zero, otherwise false. Example 1: Ruby # Ruby program to demon 1 min read How to Make a Custom Array of Hashes in Ruby? Prerequisites: Hashes and Arrays in Ruby Arrays and hashes are data structures that allow you to store multiple values at once. In this article, we will explore their syntax, how to combine functionalities of both to create an array of hashes, retrieve values, and loop through them. An array is a co 3 min read Like