2015-10-01 61 views
-2

嗨我有一个从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 

请帮忙

编辑

对不起,我是新来的红宝石,这里是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 [email protected]_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 
+1

您的字符串包含这是引号。除非您显示“@ hash_httpd”的外观(或至少是@hash_httpd ['Alias']'),否则我们无法告诉任何有关此原因的信息,也无法解决问题。 – Amadan

+0

您的编辑仍然不显示该值。我只能假设在'httpd_file_content'中有一行像'Alias'alias/public/html/ab1/ab2 /'';您的代码不会去除引号,并且它们按原样保留。如果你的文件说的是'Alias alias/public/html/ab1/ab2 /',那么你就不会有这个问题。鉴于您不分青红皂白地使用'split',这些引用具有误导性,并且完全无用(例如,它们不会像在shell中那样保护值在空间上分裂)。 – Amadan

+0

嗨阿马丹,我发现字符串来自哪里,是的,他们是你说的,其中包含引号,我试图使用gsub,但它似乎并没有把它拿出来 –

回答

1

如果你确信你的alias_path输出为"alias/public_html/ab1/ab2/",那么你可以修改你的alias_path方法通过从返回的路径中删除引号(如果有)来定义:

def alias_path(path) 
    @hash_httpd['Alias'][path].gsub('"', '') 
end