Iterating Over Strings
Part of the Fundamentals section of Coddy's Ruby journey — lesson 67 of 88.
Just like arrays, strings in Ruby can be iterated over character by character. The each_char method lets you process every character in a string individually.
Here's how it works:
word = "Ruby"
word.each_char do |char|
puts char
endThis outputs each character on a separate line: R, u, b, y. The block variable char holds one character at a time as the iterator moves through the string.
If you need both the character and its position, use each_char.with_index:
word = "Hi"
word.each_char.with_index do |char, index|
puts "#{index}: #{char}"
endThis outputs:
0: H
1: iAnother useful method is chars, which converts a string into an array of its characters. This lets you use all the array methods you've already learned:
letters = "abc".chars
puts letters # Outputs: a, b, cThese methods are helpful when you need to analyze text, count specific characters, or transform strings character by character.
Challenge
EasyRead a single line of input containing a string (e.g., hello).
Use each_char.with_index to iterate through the string and print each character along with its position. Each line should follow this format: Position 0: h
After printing all characters with their positions, print the total number of characters in the string on a new line in this format: Total characters: 5
For example, if the input is hello, the output should be:
Position 0: h
Position 1: e
Position 2: l
Position 3: l
Position 4: o
Total characters: 5If the input is Ruby, the output should be:
Position 0: R
Position 1: u
Position 2: b
Position 3: y
Total characters: 4If the input is code, the output should be:
Position 0: c
Position 1: o
Position 2: d
Position 3: e
Total characters: 4If the input is A, the output should be:
Position 0: A
Total characters: 1Cheat sheet
Use each_char to iterate over each character in a string:
word = "Ruby"
word.each_char do |char|
puts char
endUse each_char.with_index to access both the character and its position:
word = "Hi"
word.each_char.with_index do |char, index|
puts "#{index}: #{char}"
endUse chars to convert a string into an array of characters:
letters = "abc".chars
puts letters # Outputs: a, b, cTry it yourself
# Read input
str = gets.chomp
# TODO: Write your code below
# Use each_char.with_index to iterate through the string
# Print each character with its position in format: Position X: char
# Print the total number of characters in format: Total characters: XThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False