Target Variable Weirdness for .split Method


I've solved this. Or at least I got CodeCademy's result evaluator to accept what I've done. However running either version of the code seems to work, but in one case the interpretation of the result fails the evaluator.

I'd love some insight on why this happens, and if my first version is actually bad Ruby.

First, the "bad" version...

puts "Gimme text! No punctuation."
text = gets.chomp.downcase

puts "Gimme keyword!"
redact = gets.chomp.downcase

text = text.split(" ")

text.each do |word|
  if word == redact
    print "REDACT "
    print word + " "

The bad line of code is: text = text.split(" ")

According to what I've read, Ruby can reassign a variable's type. But in this case we're reassigning a string type to an array of strings type. Interestingly, the output looks perfect, leading me to assume the type reassignment worked. However the evaluator doesn't like the result and complains.

To fix the code I use a new variable: words = text.split(" ")
And of course I need to change the next line too: words.each do |word|

As best I can tell, the output of the two versions of the program are exactly the same. Is this a bug in the Ruby interpreter, a bug in the results evaluator, or a bug in something else?

I first ran into this in Redacted 4/6, but I also see it in Redacted 5/6.



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