2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ActiveSupport::OrderedOptions をYAMLに変換出来なくて、生きているのが辛い

Posted at

とりあえず、to_yamlで

hoge = ActiveSupport::OrderedOptions[:foo => 1]
puts hoge.to_yaml
--- !ruby/object:ActiveSupport::OrderedOptions {}

_人人人人人人人人_
> ご覧の有様だよ! <
 ̄Y^Y^Y^Y^Y^Y^Y ̄

Hashに変換してから to_yaml

puts hoge.to_h.to_yaml
---
:foo: 1

_人人人人人人人_
> 先頭のゴミ :
 ̄Y^Y^Y^Y^Y^Y ̄

とりあえずこれは後で考える。

ネストした ActiveSupport::OrderedOptions

hoge =
  ActiveSupport::OrderedOptions[
    :foo =>
      ActiveSupport::OrderedOptions[:bar => 1, :baz => 2],
    :zot =>
      ActiveSupport::OrderedOptions[:qux => 3, :quux => 4],
  ]
puts hoge.to_h.to_yaml
---
:foo: !ruby/object:ActiveSupport::OrderedOptions {}
:zot: !ruby/object:ActiveSupport::OrderedOptions {}

_人人人人人人人人_
> 振り出しに戻る <
 ̄Y^Y^Y^Y^Y^Y^Y ̄

もうこれでいいんじゃない?

無駄だけど、先頭のゴミ (:) も無くなったし…

puts JSON.parse(hoge.to_json).to_yaml
---
foo:
  bar: 1
  baz: 2
zot:
  qux: 3
  quux: 4

最初のハイフンが邪魔なら

削ればいいと思うよ。

puts JSON.parse(hoge.to_json).to_yaml.sub(/\A-+\n/, "")
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?