Ruby

Ruby Join an Array of Strings

Strings are a sequence of alphanumeric and special symbols. They are a crucial building block in all programming languages.

Arrays, on the other hand, are a collection of ordered and indexed elements. Elements in an array can be of any object type, such as hash, integers, strings, symbols, and more.

As the name suggests, an array of strings is an array made up of string objects and no other type.

In this guide, we will learn how to work with arrays of strings and apply various methods such as join to combine the elements in the collection.

How to Create an Array of strings

If you want to create an array of strings, you can use various methods. The most apparent is the default array creation method, which is:

arr_str = ["Python", "Ruby", "PHP", "C#", "Go", "JavaScript"]

The above method will create an array of strings separated by commas.

Using Percent String

A better way to create an array of strings is to use the percent string notation. Below is an example:

arr_str = %w{Python Ruby PHP C# Go JavaScript}

The above syntax uses the percentage notation (%w) followed by the items to add to the array separated by whitespace.

Ruby will take all the elements and convert them to an array.

print arr_str
["Python", "Ruby", "PHP", "C#", "Go", "JavaScript"]

Instead of using a pair of curly braces, you can use other matching pairs such as:

  1. Parenthesis – %w()
  2. Square brackets – %w[]
  3. Angled brackets – %w<>
  4. Exclamation marks – %w!!
  5. Pound sign – %w##
  6. At symbol – %[email protected]@

Examples:

parenthesis = %w(Python Ruby PHP C# Go JavaScript)
square = %w[Python Ruby PHP C# Go JavaScript]
angled = %w<Python Ruby PHP C# Go JavaScript>
exclamation = %w!Python Ruby PHP C# Go JavaScript!
pound = %w#Python Ruby PHP C\# Go JavaScript#
at = %[email protected] Ruby PHP C# Go [email protected]

If you have a string containing whitespace or a special character, you can use Ruby escape characters to ignore it.

How to Join an Array of Strings

You can join the elements in an array string using the join method. The method accepts two parameters: an array and a separator.

my_array = %w{Python Ruby PHP C# Go JavaScript}
puts my_array.join(";")

In the example above, we join the elements in the array and separate them with a semicolon.

The resulting value:

Python;Ruby;PHP;C#;Go;JavaScript

Let’s take an example array that contains valid SQL queries. Using the join method, you can create a combined query as:

sql_queries =  [
   "SELECT * FROM table_name",
   "SELECT column FROM table",
   "SELECT * FROM  table WHERE name = 'James'",
   ""
]
combined_query = sql_queries.join(";")
puts combined_query

The example above will separate the queries with a semicolon, rendering them a valid combined SQL query.

SELECT * FROM TABLE_NAME;SELECT COLUMN FROM TABLE;SELECT * FROM  TABLE WHERE name = 'James';

If you do not specify a delimiter for the join method, the method will return the characters for all the strings in the array.

langs = %w{Python Ruby PHP C# Go JavaScript}
combined_query = langs.join
puts combined_query

Output:

PythonRubyPHPC#GoJavaScript

The above example is similar to using the inject method as:

langs = %w{Python Ruby PHP C# Go JavaScript}
combined_query = langs.reduce(:+)
puts combined_query

Closing

This guide discussed how to work with an array of strings and combine them to create a joined array of strings.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list