2015-10-01 63 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