作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
如何使用 OpenStruct 的 marshal_load 实用程序?它似乎没有按预期工作。
文档给出 this example , 但它似乎不起作用。
require 'ostruct'
event = OpenStruct.new
hash = { 'time' => Time.now, 'title' => 'Birthday Party' }
event.marshal_load(hash)
event.title # => nil
如果不是这种方式,我如何将散列加载到 OpenStruct 中(不使用构造函数)?
对于上下文:我正在从 YAML 文件中加载哈希并将其加载到 OpenStruct 子类的现有实例中。
最佳答案
尝试使用基于符号的哈希。这对我有用。
#works.rb
hash = { :time => Time.now, :title => 'Birthday Party' }
event.marshal_load(hash)
关于ruby - 将 marshal_load 与 OpenStruct 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14331371/
如何使用 OpenStruct 的 marshal_load 实用程序?它似乎没有按预期工作。 文档给出 this example , 但它似乎不起作用。 require 'ostruct' even
我在 OSX 上的 Ruby 2.0.0 (rvm) 下运行 Rails 3.2.14。当我尝试访问大多数方法的 ri 文档时,我得到以下堆栈跟踪。 堆栈跟踪: /Users/daryl/.rvm/g
我是一名优秀的程序员,十分优秀!