不确定我是否正确使用Ruby中的Nokogiri

时间:2011-09-07 20:24:58

标签: ruby ruby-on-rails-3 nokogiri

我的代码返回这个:\“1.12.0.1058 \”我不确定我是否应该手动剥离\或什么.gsub不会剥离角色。

代码:

response = RestClient.get(@deathburritoapp_url)
        xml_doc = Nokogiri::XML(response)
        @app_packages = xml_doc.xpath("//applicationPackages//applicationPackage")
        if @app_packages != nil
          @app_packages.each do |app_pack|
            @package_id = app_pack.attribute("id").text.to_i
            @version    = app_pack.at_xpath("./version").text
            @version    = @version.gsub(/"\\/, ' ')
            @result     = "Package Id: " + @package_id.inspect + "; Version: " + @version.inspect
            Rails.logger.debug("Results: " + @result.inspect)
          end

XML:

<applicationPackages>
  <applicationPackage id="229">
    <application key="deathburrito" id="2">
        <name>deathBURRITO</name> 
    </application>
    <version>0.13.1 SNAPSHOT</version> 
    <packageType id="1">
        <name>war</name> 
    </packageType>
    <architecture id="5">
        <name>java</name> 
    </architecture>
  </applicationPackage>
  <applicationPackage id="98">
      <application key="deathburrito" id="2">
        <name>deathBURRITO</name> 
      </application>
      <version>1.10.0 SNAPSHOT</version> 
      <packageType id="1">
        <name>war</name> 
      </packageType>
      <architecture id="5">
        <name>java</name> 
      </architecture>
  </applicationPackage>
</applicationPackages>

Debug的结果:

结果:“包装ID:229;版本:\”0.13.1-SNAPSHOT \“”

1 个答案:

答案 0 :(得分:3)

那只是inspect做的事情:

ruby-1.9.2-p180 :002 > "foo".inspect
 => "\"foo\"" 

版本只是一个普通的ruby字符串,你不需要做任何特殊的事情。