【问题标题】:What do I need to do to get Hash.from_xml() to work?我需要做什么才能让 Hash.from_xml() 工作?
【发布时间】:2012-07-13 19:39:39
【问题描述】:

我在代码中安装了“ActiveSupport”并要求使用“active_support”,但是当我尝试使用 Hash.from_xml() 方法时出现“无方法错误”。

我错过了什么?

$ gem list

返回:

\*** LOCAL GEMS \***

activesupport (3.2.6)  
bundler (1.1.4)  
i18n (0.6.0)  
json (1.7.3)  
mime-types (1.19)  
multi_json (1.3.6)  
rake (0.9.2.2)  
rest-client (1.6.7)  
rubygems-bundler (1.0.3)  
rvm (1.11.3.5)

还有:

$ ruby -v

返回:

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

file.rb的内容是:

require 'active_support'  
require 'rest-client'  
require 'json'  
token = "xxx"  
user = "xxx"  
survey_id = "xxx"  
responses_from_api = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php?Request=getLegacyResponseData&User=#{user}&Token=#{token}&Version=2.0&SurveyID=#{survey_id}&Format=XML"  
responses = Hash.from_xml(responses_from_api).to_json  
puts responses

还有:

$ ruby file.rb

返回:

file.rb:8:in `<main>': undefined method `from_xml' for Hash:Class (NoMethodError)

【问题讨论】:

    标签: ruby xml hash activesupport


    【解决方案1】:

    只是require 'active_support' 做的不是很多,你必须手动拉入碎片。如果你想要所有的 ActiveSupport:

    require 'active_support/all'
    

    或者如果您只想要哈希扩展:

    require 'active_support/core_ext/hash'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-22
      • 2019-11-15
      • 2015-03-30
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多