Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlphonse Cuccurullo
2,513 PointsHi im having some real trouble with redacting can anybody help me?
puts "Enter text"
text = gets.chomp
puts"declare the redact"
redact = gets.chomp
words = text.split(",")
words.each do |d|
puts d
if word == redact
print "REDACTED"
else
print word +" "
end
So its telling me this >>> "Add an if/else statement inside your .each.
if the current word equals the word to be redacted, then print "REDACTED " with that extra space. Otherwise (else), print word + " ". The extra space in both cases prevents the words from running together."
It even gave a hint but i still don't follow heres the hint >>>"Remember, if/else looks like this:
if condition take this action else take this other action end"
1 Answer
Steve Hunter
57,712 PointsHi Alphonse,
Is this a Treehouse challenge or is the question available online somewhere?
At first glance, you aren't using your loop properly. The variable words
holds a load of words that is created by splitting the user input at each comma. I don't know if that's right, but that's what it is doing. You want to loop through those words, testing to see if each word is the same as the redacted words, outputting either the word, or "redacted".
You've started the loop correctly; words.each do |d|
starts a loop. At each iteration the individual word will be stored in the variable d
. I think you want to test if d == redact
, not word
. Indeed, word
doesn't exist so won't work for you.
You also need another end
as you need one for the if
and another for the each
loop.
Let me know how you get on with that.
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThis seems to output what you are expecting, I think:
I changed the
split
character to be a space and amended the loop variable, as above.Alphonse Cuccurullo
2,513 PointsAlphonse Cuccurullo
2,513 PointsYes it's from code academy. And thank you it works fine. Just curious what does redacted mean?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsRedacted is usually used when official documents are released. Some parts are "redacted" if the general public can't know the content. Like censored, secret or withheld. Often, names of agents or decision makers remain anonymous by way of the text of their names being redacted.