哪些与 Ruby YAML 中的 Marshal 方法 _load 和 _dump 等效?

发布于 2024-11-30 02:18:18 字数 691 浏览 5 评论 0原文

我正在使用标准 YAML 库,我有一个对象,在转储时将其转换为哈希,并在加载时从哈希转换。在 Marshal 中,我使用了 _load 和 _dump 方法重载,但 Marshal 不是人类可读的 =/

我想要自动加载对象的东西,比如 Marshal =/

类似这样的东西:

class Foo
    def initialize(numbers)
        @numbers = numbers
    end

    def to_yaml
        dump = {}
        @numbers.each {|k, v| dump[k.to_s] = v.to_s}
        dump.to_yaml
    end

    def self.from_yaml(dump)
        dump = YAML.load(dump)
        numbers = {}
        dump.each {|k, v| numbers[k.to_sym] = v.to_sym}
        new(numbers)
    end
end

bar = Foo.new({:one => :uno, :two => :dos, :three => :tres})
bar_yaml = bar.to_yaml
var = Foo.from_yaml(bar_yaml)
p var

但不太明确

I'm using the standard YAML library, I have an object that I convert to a hash when dumping it, and I convert from a hash when loading. In Marshal I used _load and _dump methods overload, but Marshal is not human-readable =/

I want something that load automatically the objects, like Marshal =/

Something like this:

class Foo
    def initialize(numbers)
        @numbers = numbers
    end

    def to_yaml
        dump = {}
        @numbers.each {|k, v| dump[k.to_s] = v.to_s}
        dump.to_yaml
    end

    def self.from_yaml(dump)
        dump = YAML.load(dump)
        numbers = {}
        dump.each {|k, v| numbers[k.to_sym] = v.to_sym}
        new(numbers)
    end
end

bar = Foo.new({:one => :uno, :two => :dos, :three => :tres})
bar_yaml = bar.to_yaml
var = Foo.from_yaml(bar_yaml)
p var

But less explicit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

战皆罪 2024-12-07 02:18:18

除非我遗漏了你问题的某些部分,否则包装上面推荐的 juwiley 方法似乎是一个非常简单的解决方案。

require 'yaml'
class Foo
  def initialize(numbers)
    @numbers = numbers
  end

  def to_yaml
    Yaml::dump(self)
  end

  def self.from_yaml(dump)
    Yaml::load(dump)
  end
end

Unless I'm missing some part of your question, it seems like wrapping the methods that juwiley recommended above would be a very simple solution.

require 'yaml'
class Foo
  def initialize(numbers)
    @numbers = numbers
  end

  def to_yaml
    Yaml::dump(self)
  end

  def self.from_yaml(dump)
    Yaml::load(dump)
  end
end
九公里浅绿 2024-12-07 02:18:18

http://corelib.rubyonrails.org/classes/YAML.html

require "yaml"

test_obj = ["dogs", "cats", "badgers"]

yaml_obj = YAML::dump( test_obj )
                    # -> ---
                         - dogs
                         - cats
                         - badgers
ruby_obj = YAML::load( yaml_obj )
                    # => ["dogs", "cats", "badgers"]
ruby_obj == test_obj
                    # => true

http://corelib.rubyonrails.org/classes/YAML.html

require "yaml"

test_obj = ["dogs", "cats", "badgers"]

yaml_obj = YAML::dump( test_obj )
                    # -> ---
                         - dogs
                         - cats
                         - badgers
ruby_obj = YAML::load( yaml_obj )
                    # => ["dogs", "cats", "badgers"]
ruby_obj == test_obj
                    # => true
〆一缕阳光ご 2024-12-07 02:18:18

在这种情况下,您可以查看 Rails ActiveRecord 序列化功能

http://api.rubyonrails。 org/classes/ActiveRecord/Base.html

我假设您想在保存后将 YAML 持久保存到数据库

In that case you might take a look at Rails ActiveRecord serialize functionality

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

I'm assuming you want to persist the YAML to DB after save

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文