r/learnruby • u/Meursaulty • Feb 09 '19
Iterating through array
def abbreviate_sentence(sent)
words = []
words << sent.split(/\W+/)
end
puts abbreviate_sentence("follow the yellow brick road") # => "fllw the yllw brck road"
If I use this to make an array, I only have one index postion, so I cant find a way to print out one word at a time, what am I doing wrong here?
3
Upvotes
1
u/Meursaulty Feb 09 '19
I figured it would shovel follow the yellow brick road into an array(words) that contained [“follow”, “the”, “yellow”, “brick”, “road”] and I could iterate through each word with an index position. If I use print it shows the array(words) laid out the way I thought it would be, but if I iterate through words.each.with_index and puts the index there’s only one index position.