Building the word array


What is wrong with the code? I keep getting this error: Oops, try again. It looks like you didn't set words equal to text.split.

puts "Please enter text: "
text = gets.chomp
words = text.split (",")


Normally, words in a sentence are separated by space characters, not commas.

words = text.split(" ")

We're telling Ruby to break up the sentence into an array of words, and discard the space characters.


I tried with and without the comma. It still did not accept my code.

Is there a reason why this code worked?

puts text = gets.chomp
text = gets.chomp

words = text.split


defaults to splitting on single spaces, but ignores leading and trailing white space.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.