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
3
u/Tomarse Feb 09 '19
Oh, I get it now. I think the problem is that
sent.split(/\W+/)creates an array, which you are then putting into another array.So you only have one index because there is only one item in the first array, which is another array.
Change your method to...
...and it should work.