Table of contents
1.
Introduction
2.
Iterating Strings in Ruby
3.
Using ‘each’ method
4.
Using ‘split’ Method
5.
Using ‘each_char’ method
6.
Using ‘each_byte’ Method
7.
Using ‘each_line’ Method
8.
Frequently Asked Questions
8.1.
Is it possible to iterate over a string in Ruby?
8.2.
In Ruby, what does each_char do?
8.3.
In Ruby, how do you append to a string?
8.4.
What is Ruby primarily used for?
8.5.
Is Ruby still utilized in today's world?
9.
Conclusion
Last Updated: Mar 27, 2024

Iterating Strings in Ruby

Author Shiva
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Here's an interesting fact: Did you know that the name Ruby was coined by Matsumoto and Keiju Ishitsuka (The Men Behind Ruby) during an internet chat session on February 24, 1993, before any code had been written in the language? Two names were considered at first: Coral and Ruby. In an email to Ishitsuka, Matsumoto chose one.

To the topic, Iterators are methods that loop over a set of data in a natural way, allowing you to operate on each individual piece in the collection.

Arrays, as previously stated, are ordered lists. Let's imagine you have a list of names that you wanted to display on the screen. How did you manage to do that? Iterating strings in ruby can be done using Arrays methods like each, each_char can be used in a variety of ways, as shown below.

Iterating Strings in Ruby

In Ruby 1.8, the String class defines each method that iterates a string line-by-line. The String class includes the methods of the Enumerable module, and they can be used to process the lines of a string. You can use the each_byte iterator in Ruby 1.8 to iterate through the bytes of a string, but there is little advantage to using each_byte over the [] operator because random access to bytes is as quick as sequential access in 1.8. The situation is quite different in Ruby 1.9, which removes each method, and in which the String class is no longer Enumerable.

In place of each, Ruby 1.9 defines three clearly named string iterators: 

  • each_byte iterates sequentially through the individual bytes that comprise a string,
  • each_char iterates the characters,
  • and each_line iterates the lines.

If you want to process a string character-by-character, it may be more efficient

to use each_char than to use the [] operator and character indexes:

Using ‘each’ method

Iterating strings in ruby is simply to go over a string character by character after cutting it up with split or using the each_char method.

We split the string in the first example by utilizing the empty string as a separator. That's all; we're going to clip the string every time we come across an empty string. To put it another way, each and every character.

input = 'Coding Ninjas'
chars = input.split('')
# printing chars arrary
puts chars.length
# accessing element using subscript
puts chars[2]
# new line
puts
chars.each { |c|
 puts c
}

 

Output:

The resulting array has 13 elements in it as returned by the length method and the third character (index 2) is the letter ‘d’. These were introduced solely to demonstrate how the split behaves like an array.

Finally, we loop over the elements of the array that correspond to the characters in the original string using ‘each' method. 

Using ‘split’ Method

Iterating strings in ruby can also be done using the same split method in the second example, but this time we call each method directly on top of the split result:

input = 'Coding Ninjas'
 
input.split('').each { |c|
 puts c
}

 

The outcome is the same as before:

Using ‘each_char’ method

Iterating strings in ruby can be even easier, Ruby has a function called each char that takes care of everything:

input = 'Coding Ninjas'

input.each_char { |c|
 puts c
}

 

Output:

Using ‘each_byte’ Method

For Iterating strings in Ruby, the each_byte method is used to transmit each character in a string's byte representation to a block.
In Ruby, a block is a loop. It just has one parameter. It takes the byte representation of each character in a string in this example.
# create some strings
str1 = "Coding Ninjas"

# print each byte 
# reprentation of a character
# convert them into character again using chr method
str1.each_byte {|b| puts (b).chr}


Output:

In the code given above, we were able to convert the byte of each character of the string str1, to the character using chr method and printing in the console.

Using ‘each_line’ Method

Iterating strings in ruby using each_line method; each_line is a Ruby String class method that splits a string using the specified parameter as the record separator ($/ by default), passing each substring to the supplied block in turn. If a zero-length record separator is provided, the string is split into paragraphs separated by multiple subsequent newlines.

# Ruby program to demonstrate
# the each_line method

# Taking a string and
# using the method
puts "Code\nStudio".each_line {|s| s}


Output:

Frequently Asked Questions

Is it possible to iterate over a string in Ruby?

Iterating Strings in Ruby is possible, The chars method can be used to turn a string into an array of characters. Then you can iterate over this array by using each.

In Ruby, what does each_char do?

Iterating Strings in Ruby can also be done using ‘each_char’ method. Each character that makes up a string is passed to a block in Ruby using the each_char function. each_char is a single argument block that represents characters in a string.

In Ruby, how do you append to a string?

To append a string to another, use the + operator. 

Eg. a + b + c creates a new string.

What is Ruby primarily used for?

The language has a wide range of applications, including web scraping, static site generation, command-line tools, automation, DevOps, and data processing.

Is Ruby still utilized in today's world?

Despite the fact that Ruby on Rails was first released about 18 years ago, the framework is still frequently utilized among professional developers.

Conclusion

This article covers everything you need to know about Iterating Strings in Ruby. Want to learn more?
Here are more articles for rescue.

Recommended problems -

Refer to our guided paths on Coding Ninjas Studio to learn more about DSACompetitive Programming, Javascript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.
Do upvote our blog to help other ninjas grow.
Happy Learning!

 

Live masterclass