【发布时间】:2015-10-01 02:37:37
【问题描述】:
您好,我有一个从 rspec 传回的字符串。
它应该显示
"alias/public_html/ab1/ab2/"
但我收到"\"alias/public_html/ab1/ab2/\""
我收到以下 rspec 错误:
WebServer::HttpdConf#alias_path returns the aliased path
Failure/Error: expect(httpd_file.alias_path('/ab/')).to eq 'alias/public_html/ab1/ab2/'
expected: "alias/public_html/ab1/ab2/"
got: "\"alias/public_html/ab1/ab2/\""
(compared using ==)
# ./spec/lib/config/httpd_conf_spec.rb:90:in `(root)'
这是我的实际程序文件
def alias_path(path)
@hash_httpd['Alias'][path]
end
请帮忙
编辑
抱歉,我是 RUby 新手,这里是 httpd_file
def initialize(httpd_file_content)
@hash_httpd = Hash.new
httpd_file_content.each_line do | line |
@commands = line.split
if @commands.length == 2
@hash_httpd[@commands[0]] = @commands[1]
else
if !@hash_httpd.has_key?(@commands[0])
al = Hash.new
@hash_httpd[@commands[0]] = al
else
al = @hash_httpd[@commands[0]]
end
al[@commands[1]] = @commands[2]
end
end
end
【问题讨论】:
-
您的字符串包含引号。除非您展示
@hash_httpd的样子(或至少@hash_httpd['Alias']),否则我们无法说出导致此问题的原因或修复方法。 -
您的编辑仍然没有显示值。我只能假设在
httpd_file_content中有一行像Alias "alias/public/html/ab1/ab2/";您的代码不会去掉引号,它们会按原样保留。如果您的文件显示类似Alias alias/public/html/ab1/ab2/的内容,则不会出现此问题。鉴于您不分青红皂白地使用split,引号具有误导性,而且完全没用(例如,它们不会保护值不被空格分割,就像在 shell 中那样)。 -
嗨,Amadan,我找到了字符串的来源,是的,正如你所说的那样,其中包含引号,我尝试使用 gsub,但它似乎没有取出来