To iterate over words in a string in Ruby, you can use the split
method with a space as the delimiter.
In this example,
str
with a value of 'Hello World Ruby'
.split(' ')
method on the string to split it into words based on spaces.str = 'Hello World Ruby'
words = str.split(' ')
words.each do |word|
puts word
end
Hello World Ruby
In this tutorial, we learned How to Iterate over Words in String in Ruby language with well detailed examples.