说我有一个字符串如下:
"auto: true; server: false;"
...我想要一个正则表达式来创建这些设置的哈希值。我有以下代码:
# class Configurer...
def spit(path = "", *args)
spat = Hash.new
if File.file?(path)
# Parse file
else
args.each do |arg|
begin
if path.include? arg + ":"
strip = path.match(/#{arg}:\s(.*);/)
spat[arg] = strip[1]
end
rescue
return "Error when parsing '#{arg}' in direct input."
end
end
end
spat
end
当像:
config = Configurer.new
puts config.spit("auto: true; server: false;", "auto", "server")
...运行,输出的哈希值不正确:
# => {"auto"=>"true; server: false", "server"=>"false"}
为什么?当我parse a file (line by line)并使用相同的正则表达式时,我得到了所需的哈希值。为什么这种方法不是这样的呢?