7 Replies - 1278 Views - Last Post: 28 April 2013 - 04:59 AM Rate Topic: -----

#1 tessierny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 12-February 13

Cannot print "REDACTED" in place of every redacted word in an

Posted 25 April 2013 - 10:37 AM

The title pretty much says it all. I cannot get my code to print "REDACTED" in place of every redact in my if loop. IF anyone can shed light, it would be appreciated! Thanks! Here is my code :

puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each {|x| print "#{x}"}
if words.include?(redact)
print "REDACTED"+" "
else
print "#{words}"+" "
end


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot print "REDACTED" in place of every redacted word in an

#2 xclite  Icon User is online

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,206
  • Joined: 12-May 09

Re: Cannot print "REDACTED" in place of every redacted word in an

Posted 25 April 2013 - 10:43 AM

You're printing all of the words, then printing:
"REDACTED "
if the string redact is in the words list.

Additionally, you should note that there is no such thing as an "if loop" - the if statement gets executed only once.
Instead, the if should be part of your each, something like:
puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each do |x|
  if x == redact
    print "REDACTED"
  else
    print x
  end
end


Another quick solution:
puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.gsub(redact, "REDACTED").split(" ")
words.each {|x| print "#{x}"}


This post has been edited by xclite: 25 April 2013 - 10:43 AM

Was This Post Helpful? 2
  • +
  • -

#3 tessierny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 12-February 13

Re: Cannot print "REDACTED" in place of every redacted word in an

Posted 25 April 2013 - 10:57 AM

Hi xclite,
Thanks for the reply. It was very helpful but is comparing x to redact, possible? I wonder if that is the same as comparing an array to a string? Since x is a placeholder I am probably incorrect on that but I am trying to follow the logic and not just the syntax. Here is my altered code which still does not work :

puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each {|x| if x == redact print "REDACTED" else print "#{x}"}



View Postxclite, on 25 April 2013 - 09:43 AM, said:

You're printing all of the words, then printing:
"REDACTED "
if the string redact is in the words list.

Additionally, you should note that there is no such thing as an "if loop" - the if statement gets executed only once.
Instead, the if should be part of your each, something like:
puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each do |x|
  if x == redact
    print "REDACTED"
  else
    print x
  end
end


Another quick solution:
puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.gsub(redact, "REDACTED").split(" ")
words.each {|x| print "#{x}"}


Was This Post Helpful? 0
  • +
  • -

#4 tessierny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 12-February 13

Re: Cannot print "REDACTED" in place of every redacted word in an

Posted 25 April 2013 - 11:07 AM

Hmm, well, your method definitely works. I tested it. Also, my version has the same syntax except for the do command. Instead of "do", I use curly brackets {}. The tutorial said that those two are the same thing and can be used inter-changably but as I found, it isn't working when you use curly brackets. Unless I am missing something important.
Was This Post Helpful? 0
  • +
  • -

#5 xclite  Icon User is online

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,206
  • Joined: 12-May 09

Re: Cannot print "REDACTED" in place of every redacted word in an

Posted 25 April 2013 - 11:14 AM

View Posttessierny, on 25 April 2013 - 01:57 PM, said:

Hi xclite,
Thanks for the reply. It was very helpful but is comparing x to redact, possible? I wonder if that is the same as comparing an array to a string? Since x is a placeholder I am probably incorrect on that but I am trying to follow the logic and not just the syntax.

By using each, you are iterating over each element in words. You're right - x is a placeholder and takes the value of each element.

Quote

Here is my altered code which still does not work :

puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each {|x| if x == redact print "REDACTED" else print "#{x}"}

{} and do/end are usually interchangeable - the convention is to use {} for one liners and do/end for multi.
You MAY need to do:
{if x == redact then print "REDACTED" else print "#{x}" end}
But I prefer to keep those on multiple lines instead.
Was This Post Helpful? 1
  • +
  • -

#6 tessierny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 12-February 13

Re: Cannot print "REDACTED" in place of every redacted word in an

Posted 25 April 2013 - 11:20 AM

Thank you so much! It now makes perfect sense to me. I really appreciate that.

View Postxclite, on 25 April 2013 - 10:14 AM, said:

View Posttessierny, on 25 April 2013 - 01:57 PM, said:

Hi xclite,
Thanks for the reply. It was very helpful but is comparing x to redact, possible? I wonder if that is the same as comparing an array to a string? Since x is a placeholder I am probably incorrect on that but I am trying to follow the logic and not just the syntax.

By using each, you are iterating over each element in words. You're right - x is a placeholder and takes the value of each element.

Quote

Here is my altered code which still does not work :

puts "Whats your input brah?"
text = gets.chomp
puts "Whatchu are you hiding bro?"
redact = gets.chomp
words = text.split(" ")
words.each {|x| if x == redact print "REDACTED" else print "#{x}"}

{} and do/end are usually interchangeable - the convention is to use {} for one liners and do/end for multi.
You MAY need to do:
{if x == redact then print "REDACTED" else print "#{x}" end}
But I prefer to keep those on multiple lines instead.

Was This Post Helpful? 0
  • +
  • -

#7 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,514
  • Joined: 28-November 09

Re: Cannot print "REDACTED" in place of every redacted word in an

Posted 27 April 2013 - 11:05 PM

If you're working with sentences then remember to use downcase on that comparison otherwise it literally compares them.

Have you ever seen the map function? It would do you quite a bit of good in this case.

puts text.split(" ").map{ |w| w = "REDACTED" if w == redacted }.join(' ')

...or something similar to that.

http://ruby-doc.org/...ml#method-i-map

Really though this is complicating things too much as is, gsub:
http://ruby-doc.org/...l#method-i-gsub

This post has been edited by Lemur: 27 April 2013 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Cannot print "REDACTED" in place of every redacted word in an

Posted 28 April 2013 - 04:59 AM

Quote

map{ |w| w = "REDACTED" if w == redacted }


That won't work. First of all the above block is equivalent to just { |w| "REDACTED" if w == redacted } because reassigning the block parameter doesn't have any effect (unless you later refer to the parameter again within the same block, which, in this case, you don't).

The main problem, however, is that that expression if condition evaluates to nil if condition is false. So if %w(a b c).map {|w| "X" if w=="b"} will evaluate to [nil, "X", nil], not ["a", "X", "c"].

To make that code work, it should look like this:

puts text.split(" ").map{ |w| if w == redacted then "REDACTED" else "w" end }.join(' ')


It would be more readable (in my opinion) to split that into multiple lines though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1