我有许多的日志文件,每个文件包含像这样的一行:红宝石检查是否字符串包含子
THIS LINE IS DIFFERENT CASE_WINDOWS_TEST_00 PASSED
我在寻找,如果行包含“_XXX_TEST_”字符串。我创建的哈希:
@@groups = {
"_LINUX_TEST_" => "Linux_tests",
"_WINDOWS_TEST_" => "Windows_tests"
}
要检查行包含(从@@基团的键)的子我实现它返回从@@基团的值的方法get_group_name。
def get_group_name(searchLine)
@@groups.keys.each do |i|
if searchLine.include? i
return @@groups[i]
end
end
end
它工作正常,返回适当的值。我在另一个遍历整个日志文件的方法中使用这个方法。
def get_group_name_from_file(fileName)
# fileName - filename or path to the file.txt
file = File.open(fileName)
while (line = file.gets)
found = get_group_name(line)
if found
return found
end
end
end
这就是问题所在。方法get_group_name_from_file返回来自@@组哈希的密钥列表,而不是一个字符串(来自该哈希的值)。
无法重现 –