我该如何重复这段代码?

时间:2017-03-23 05:50:43

标签: ruby string search replace substring

我是初学程序员。我的代码在Ruby中,基本上扫描文本并查找某个单词出现的次数或替换文本中的单词。我无法做的第一件事就是重复代码块。如果有人想要重新扫描或替换他们的文本,他们必须重新启动程序而不是正在进行的程序,我已经尝试通过获取整个代码块并循环它来解决这个问题,但它没有工作并返回一个错误。另一件事是每当我使用替换函数时,它只替换一个单词而不是两个单词。例如,它在下图中显示了食物模因而不是食物食物:

puts "insert your text: "
string = gets.chomp

puts "would you like to scan or replace your text?"
choice = gets.chomp

#this is the scan option which scans the text and outputs the amount of times a word is in it

if choice.include? "scan"

  puts "what word would you like to scan?"
  word_for_scan = gets.chomp
      scan_count = string.scan(word_for_scan).count
      puts "The word #{word_for_scan} appears #{scan_count} times"

#this is the replace function in the code which replaces the words in the text, as shown in the image above, whenever I try to replace something it only replaces one word instead of all of them

elsif choice.include? "replace"

  puts "what word would you like to replace?"
  word_for_replacement = gets.chomp
      puts "what word would you like to replace it with?"
      replacement_word = gets.chomp
        replaced_text = string.sub(word_for_replacement, replacement_word)
        puts "here is your new text: #{replaced_text}"

else
  puts "error: would you like to scan or replace text?"

end

感谢您查看代码并尝试解决问题。

3 个答案:

答案 0 :(得分:2)

1) 回答问题1的递归执行

只需将代码移至method/function 并调用其中的same function

这称为概念recursive function并满足您的要求。

def changeText

  puts "insert your text: "
  string = gets.chomp

  puts "would you like to scan or replace your text?"
  choice = gets.chomp

  #this is the scan option which scans the text and outputs the amount of times a word is in it

  if choice.include? "scan"

    puts "what word would you like to scan?"
    word_for_scan = gets.chomp
        scan_count = string.scan(word_for_scan).count
        puts "The word #{word_for_scan} appears #{scan_count} times"

  #this is the replace function in the code which replaces the words in the text, as shown in the image above, whenever I try to replace something it only replaces one word instead of all of them

  elsif choice.include? "replace"

    puts "what word would you like to replace?"
    word_for_replacement = gets.chomp
        puts "what word would you like to replace it with?"
        replacement_word = gets.chomp
          replaced_text = string.sub(word_for_replacement, replacement_word)
          puts "here is your new text: #{replaced_text}"

  else
    puts "error: would you like to scan or replace text?"

  end
  changeText();
end  

changeText();

2) 对问题2的回答使用gsub代替sub

gsub替换所有实例,而sub替换单个实例。

string.gsub(word_for_replacement, replacement_word)

答案 1 :(得分:1)

只需在代码周围加一个while循环。

while true do
    puts "hello"
end

Control-C将退出循环,或者您可以根据用户的输入做一些更奇特的事情。

您可以使用gsub代替sub

替换字符串中的所有实例

答案 2 :(得分:1)

如何让您的程序反复运行
如果您不熟悉while循环或定义函数,请继续阅读所有相关内容。我不想破坏发现它可以为你的程序做些什么的乐趣。但是,如果你真的需要你的程序立即工作而不深入细节,检查其他答案,你会找到正确的代码!

如何替换所有单词
在替换给定单词的所有实例时,您希望使用gsub方法而不是sub

    replaced_text = string.gsub(word_for_replacement, replacement_word)

sub替换一个单词的一个出现,而gsub用于全局替换(所有出现)。