MCP_infiltrator
MCP_infiltrator

Reputation: 4179

Redacted Iterator in Ruby

I am going through a Ruby course on Codecademy.com. The problem is listed under Iterators and Loops. Here are the instructions:

Let's start simple: write an .each loop that goes through words and just prints out each word it finds.

Here is what I have which does not seem to pass the test, so I just want to know if it is correct or not.

puts "need input please"
text = gets.chomp

words = text.split(" ")
words.each do |x|
  puts "#{x}"
end

puts "need another input"
redact = gets.chomp

Somewhat oddly, this is what passed the course example

puts "need input please"
text = gets.chomp
words = text.split(" ") 
redact = gets.chomp

Which is obcviously not right since it does not make use of the .each loop.

Upvotes: 1

Views: 2349

Answers (2)

Debonemo
Debonemo

Reputation: 11

hello im doing the same course on codecademy and wrote this even though it lets me pass im unsure if its correct if this helps

puts "Enter paragraph here: "
text=gets.chomp
puts "Word to redact"
redact = gets.chomp
words = text.split {" "}
words.each do |words|

  if words == redact
    print "REDACTED "
  else 
    print words + " "
  end
end

Upvotes: 0

AGS
AGS

Reputation: 14498

words.each do |x|
  puts "#{x}"
end

x is your block variable, and doesn't need to be interpolated.

Try

words.each do |x|
  puts x
end

Upvotes: 1

Related Questions