奇怪的行为破坏了字符串的某些部分,但只有特定的字符,Atom,RSpec

Strange behaviour corrupting some part of a string but only particular chars, Atom, RSpec

我遇到了一个奇怪的情况。我有一个函数,它遍历一个字符串数组并在 "is" 上拆分每个字符串,这些字符串正在 RSpec 中进行测试,如下所示:

测试体

info_combo = ["pish pish Iron is 3910 Credits","glob prok Gold is 57800 Credits"]
expect(interpreter.solveForUnknownInfo(info_combo)).to eq some_final_expectable_object

函数

def getSubjectsAndObjects(info_combo)
   subjects = []
   objects = []
  info_combo.each do |info_str|
    print info_str
    subjectsAndObjects = info_str.split("is")
    print subjectsAndObjects
    subjects << subjectsAndObjects[0]
    objects << subjectsAndObjects[1]
  end
return subjects, objects
end

调试时的打印输出

"pish pish Iron is 3910 Credits" => first iteration input
["p", "h p", "h Iron ", " 3910 Credits"] => crazy unexpected
"glob prok Gold is 57800 Credits"   => second iteration input
["glob prok Gold ", " 57800 Credits"] => expectable output

## 替换第二个输入字符串的第一个子字符串后,'pish' 为 'another_random_word' ...

"another_random_word pish Iron is 3910 Credits" => first iteration input
["another_random_word p", "h Iron ", " 3910 Credits"] =>some hopeful change
"glob prok Gold is 57800 Credits" => second iteration input
["glob prok Gold ", " 57800 Credits"] => expectable output

## 将最后的 'pish' 替换为 'another_random_word'

"another_random_word another_random_word Iron is 3910 Credits" => first iteration input
"another_random_word another_random_word Iron ", " 3910 Credits"] => now totally expectable/desired output from function
"glob prok Gold is 57800 Credits" => second iteration input
["glob prok Gold ", " 57800 Credits"] => expectable output

这让我很困惑。我不知道如何调试这个或可能出问题的想法。我认为这是一个文本编辑器故障(Atom),重新启动了程序并且没有任何变化。

我错过了什么?有任何想法吗?也非常欢迎关于改进 question/title 的想法。

您错过了相当简单的事情:"pish" 的中间两个字符是 "is"。所以当然,如果你在 "is" 上拆分,它会拆分为 "p""h"

有几种解决方法。在您的情况下,最简单的方法可能是在 " is " 上拆分(即 "is" 两边各有一个 space)。根据确切的需要,您可以改为拆分正则表达式,例如 /\sis\s/("is" 两边都有某种 whitespace on either side, could be space, tab, etc) or /\bis\b/ ("is" with a word boundary - 在这种情况下,"is" 不能' t 位于单词的中间,但周围的白色 space 实际上不是匹配项的一部分,因此不会从字符串中删除。

 "his is hers".split(/\sis\s/) # => ["his", "hers"]
 "his is hers".split(/\bis\b/) # => ["his ", " hers"]

请注意,在第一种情况下,space 是定界符的一部分并与其一起被删除,但在第二种情况下,它们不是定界符的一部分,因此不会被删除。