5/6 shouldn't this work?


#1

puts "Please enter some text!"
text= gets.chomp
puts "Enter text to censor!"
redact= gets.chomp
words=text.split(" ")
words.each do |word|
if words=redact
print "REDACTED "
else
print words + " "
end
end

This just "redacts" each word. do I have to switch around the if and else statements for it to work? or have I got a syntax error?

Many Thanks!


#2

First of all, please place a whitespace between operands and a operator:

operand = operator
operand > operator

etc

That is standard coding style.

Also, next time please use the </> code tag to format your code. That will preserve indentation in your code, another aspect of proper programming style.

In the expression for the if statement, you had the wrong name for the variable: it is word and not words. In addition, you used the assignment operator instead of the comparison operator. You use a single equal sign, =, for assignment operation. To compare things, use the double equals sign, ==. In Ruby, the double == will check for both equal type AND equal value.

puts "Please enter some text!"
text = gets.chomp
puts "Enter text to censor!"
redact = gets.chomp

words = text.split(" ")

words.each do |word|
  # pay attention the following line:
  if word == redact 
    print "REDACTED "
  else
  # you used the wrong variable name below (words is incorrect)
    print word + " "
  end
end

#3

All the same.......... -your- code (when pasted) results in a "fail" ... lol, which is odd, because I had about the same idea. Weird?


#4

After further experimentation, turns out you DO need that "extra" 'end' @ajaxwhiz48261 had in there.

silly.


#5

You're right. Sorry about that. I failed to look at the do at the end of the statement. They typically ask for an end.


#6

I used the code

words.each do |word|
# pay attention the following line:
if word == redact
print "REDACTED " ******THERE HAS TO BE A SPACE AFTER REDACTED!!*******
else
# you used the wrong variable name below (words is incorrect)
print word + " "
end
end

It did not work till I added the space after redacted. how dumb.