Ruby 对于将字符串更改为结尾的循环,如何满足此条件?
我将一个单词与另一个字符串进行比较,通过循环字母表并在单词的每个位置插入每个字母,字符串会发生变化Ruby 对于将字符串更改为结尾的循环,如何满足此条件?,ruby,string,loops,Ruby,String,Loops,我将一个单词与另一个字符串进行比较,通过循环字母表并在单词的每个位置插入每个字母,字符串会发生变化 @position_counter = 0 编辑:下面是字母_循环正在运行的代码 @array = ["amethod", "variable", "block"] def word_list_loop @match_counter = 0 @array.each do |word| letter_loop(word) end puts @match_counter e
@position_counter = 0
编辑:下面是字母_循环正在运行的代码
@array = ["amethod", "variable", "block"]
def word_list_loop
@match_counter = 0
@array.each do |word|
letter_loop(word)
end
puts @match_counter
end
关闭编辑
def letter_loop(word)
("a".."z").each do |letter|
word_plus_letter = @word.dup
word_plus_letter.insert(@position_counter, letter)
@match_counter+=1 if word.match(/\A#{word_plus_letter}\z/)
end
@position_counter+=1
letter_loop(word) unless @position_counter == (@word.length + 1)
end
我用于参数的单词是“method”
。但是当我运行这个程序时,我得到了一个索引7(IndexError)
。它正确地在每个位置的字母表中循环,但似乎不会被抓住,除非@position\u counter==(@word.length+1)
结束
我尝试了其他几种方法,如if语句等,但我无法让该方法自行完成。您运行了多少次
字母循环?您确定错误发生在第一次运行时吗?据我所见,如果您第二次调用它时没有将@position\u counter
重置为零,它将以@word.length+1
开始,产生您所看到的确切错误。除此之外,我没有发现您的代码有任何问题(在第一次运行时运行得很好)
更新:由于您使用的是递归解决方案,position\u counter
并不表示程序的状态(仅表示方法调用的状态),因此我建议不要将其声明为@position\u counter
,而是作为方法的可选参数:
def letter_loop(word, position_counter=0)
("a".."z").each do |letter|
word_plus_letter = @word.dup
word_plus_letter.insert(position_counter, letter)
@match_counter+=1 if word.match(/\A#{word_plus_letter}\z/)
end
position_counter+=1
letter_loop(word, position_counter) unless position_counter == (@word.length + 1)
end
如果您不能/不想这样做,只需在每次使用之前/之后重置它,就像我之前建议的那样,它就会正常工作:
@array.each do |word|
@position_counter = 0
letter_loop(word)
end
(虽然我不推荐第二种方法,因为如果您忘记在其他地方重置它,您的方法将再次失败)您运行了多少次字母循环
?您确定错误发生在第一次运行时吗?据我所见,如果您第二次调用它时没有将@position\u counter
重置为零,它将以@word.length+1
开始,产生您所看到的确切错误。除此之外,我没有发现您的代码有任何问题(在第一次运行时运行得很好)
更新:由于您使用的是递归解决方案,position\u counter
并不表示程序的状态(仅表示方法调用的状态),因此我建议不要将其声明为@position\u counter
,而是作为方法的可选参数:
def letter_loop(word, position_counter=0)
("a".."z").each do |letter|
word_plus_letter = @word.dup
word_plus_letter.insert(position_counter, letter)
@match_counter+=1 if word.match(/\A#{word_plus_letter}\z/)
end
position_counter+=1
letter_loop(word, position_counter) unless position_counter == (@word.length + 1)
end
如果您不能/不想这样做,只需在每次使用之前/之后重置它,就像我之前建议的那样,它就会正常工作:
@array.each do |word|
@position_counter = 0
letter_loop(word)
end
(尽管我不推荐第二种方法,因为如果您忘记在其他地方重置它,您的方法将再次失败)我认为问题在于您正在从@数组中调用字母循环,但在@数组的每次迭代中,都不会将@position\u计数器
重置为零。每个
循环
如果这不能解决您的问题,请在字母循环的第一行添加类似的内容:
puts "letter_loop word=#{word}, position=#{@position_counter}, matches=#{@match_counter}"
然后运行程序并检查指向索引器的输出
我认为问题在于您正在从@数组中调用字母循环
。每个
,但在@数组的每次迭代中都没有将@位置计数器
重置为零。每个
循环
如果这不能解决您的问题,请在字母循环的第一行添加类似的内容:
puts "letter_loop word=#{word}, position=#{@position_counter}, matches=#{@match_counter}"
然后运行程序并检查指向索引器的输出
没错,我没有单独运行这个。问题似乎出现在运行的代码字母循环中。我刚刚用代码更新了问题,就这样!但是,我建议改为使用可选参数,这样您就不会忘记在某个地方重置position\u counter
。查看我的更新答案。你是对的,我没有单独运行这个。问题似乎出现在运行的代码字母循环中。我刚刚用代码更新了问题,就这样!但是,我建议改为使用可选参数,这样您就不会忘记在某个地方重置position\u counter
。请参阅我的最新答案。