-1

I need to replace placeholders NOUN, VERB, ADJ, and ADV in a file solution09.txt with user input.

Madlib solution09.txt:

One day I was watching my son [ADV] play with his [NOUN]. He was pretending the [NOUN] were [ADJ]. After a few minutes he was pretending to [VERB], because one of the [NOUN] drove away. When i asked him about it he [ADV] said, umm it's funny when [NOUN] [VERB] because [NOUN] can't really [VERB].

I think I successfully put the file into a string, but I have to read the string, and replace the placeholders with user input. Once I replace, I need to output the new madlib. I'm getting the user input into variables, but I'm not sure how to correctly replace the placeholder with the users input.

Current code:

file = File.open("solution09.txt", "r")
contents = file.read
puts "Enter a noun: "
noun = gets.chomp

puts "Enter a verb: "
verb = gets.chomp

puts "Enter an adjective: "
adj = gets.chomp

puts "Enter an adverb: "
adv = gets.chomp

if file.include?('NOUN')
  file1= file.gsub("[NOUN]", noun, "[VERB]", verb, "ADJ", adj, "ADV", adv)
end
sawa
  • 165,429
  • 45
  • 277
  • 381
Nate_LADE
  • 59
  • 10
  • 1
    First issue I see would be that gsub doesn't quite work like that. If you want to do multiple substitutions, then you need to make multiple calls to gsub, like `new_string = old_string.gsub('[NOUN]', noun).gsub('[VERB]', verb)` You can string as many gsubs as you want like that. Second issue is that you're inconsistent with your [] surrounding the parts of speech. NOUN and VERB have them, ADJ and ADV don't. Third issue is that you're calling gsub on `file`, which is the file object, rather than `contents`, which is a string that represents your data. That should be enough to get started. – Some Guy Jun 06 '16 at 02:40
  • That is exactly what I needed to get it to output correctly. Thank you! – Nate_LADE Jun 06 '16 at 02:46

3 Answers3

1

You can also build a replacement hash:

filename = "solution09.txt"
contents = File.read(filename)
replacements = {}

puts "Enter a noun: "
replacements['[NOUN]'] = gets.chomp

puts "Enter a verb: "
replacements['[VERB]'] = gets.chomp

puts "Enter an adjective: "
replacements['[ADJ]'] = gets.chomp

puts "Enter an adverb: "
replacements['[ADV]'] = gets.chomp

And pass it to gsub:

contents.gsub(Regexp.union(replacements.keys), replacements)

Regexp.union creates a pattern that matches any of the given keys.

Stefan
  • 109,145
  • 14
  • 143
  • 218
0

Your code should look like

filename = "solution09.txt"
contents=File.read(filename)

puts "Enter a noun: "
noun=gets.chomp

puts "Enter a verb: "
verb=gets.chomp

puts "Enter an adjective: "
adj=gets.chomp

puts "Enter an adverb: "
adv=gets.chomp

if contents.include?('NOUN')
  { "\[NOUN\]" => noun,
    "\[VERB\]" => verb,
    "\[ADJ\]" => adj,
    "\[ADV\]" => adv
  }.each do |key, value|
    contents.gsub!(key, value)
  end
  File.open(filename, "w") { |f| f << contents }
end

You need separate operation for read and write. There are other ways to do this

You can see how to do with single file pointer https://stackoverflow.com/a/10173112/1380263

You can also use ruby methods which interact with shell and use sed command (system, backticks, peopen)

Really depends on what suits your situation the best

Community
  • 1
  • 1
Garima Singh
  • 233
  • 1
  • 7
  • 1
    There are so many ways to code to get the same results. This is another example of how every one has their own way of coding. – Nate_LADE Jun 06 '16 at 03:23
0
file = File.read("solution09.txt")
.gsub(/\[(NOUN|VERB|ADJ|ADV)\]/) do
  part = case $1
  when "NOUN" then "a noun"
  when "VERB" then "a verb"
  when "ADJ" then "an adjective"
  when "ADV" then "an adverb"
  end
  puts "Enter #{part}: "
  gets.chomp
end
sawa
  • 165,429
  • 45
  • 277
  • 381
  • This is a great way to shorten up my code. Thank you for providing your solution! – Nate_LADE Jun 06 '16 at 03:24
  • 1
    This is not equivalent to the OPs code. It will prompt for every single replacement. (BTW, your `case` statement is missing an `end`) – Stefan Jun 06 '16 at 05:08
  • 1
    @Stefan Thanks for pointing out. I realized that it was not the same, but I wasn't sure about the OP's intention. – sawa Jun 06 '16 at 05:26