String#chars
str.chars -> array Returns:
Array · Updated March 13, 2026 · String Methods strings characters arrays
The .chars method breaks a string into an array of individual characters. Each character becomes a separate string element in the returned array, making it easy to iterate over, count, or transform individual characters.
Syntax
string.chars
Parameters
.chars takes no parameters. The method operates on the string it’s called on.
Examples
Basic usage
"hello".chars
# => ["h", "e", "l", "l", "o"]
"Ruby".chars
# => ["R", "u", "b", "y"]
Working with Unicode
"café".chars
# => ["c", "a", "f", "é"]
"🎉🎊".chars
# => ["🎉", "🎊"]
Common Patterns
Counting characters
word = "antidisestablishmentarianism"
counts = word.chars.tally
# => {"a"=>6, "n"=>4, "t"=>5, "i"=>5, "d"=>2, "s"=>4, "e"=>4, "l"=>3, "h"=>1, "m"=>2, "r"=>1}
Iterating over characters
"hello".chars.each { |c| puts c }
# h
# e
# l
# l
# o
Transforming characters
"hello".chars.map(&:upcase)
# => ["H", "E", "L", "L", "O"]
Checking for character presence
vowels = ["a", "e", "i", "o", "u"]
word = "education"
has_vowel = word.chars.any? { |c| vowels.include?(c) }
# => true
Performance Considerations
For very large strings, .chars creates a new array containing every character, which uses memory proportional to the string length. For simple iteration, consider using .each_char instead:
# More memory-efficient for large strings
"very long string".each_char { |c| process(c) }