LoginSignup
0
0

More than 1 year has passed since last update.

Ruby | YAML.safe_load で Datetime型のオブジェクトを復元できない

Last updated at Posted at 2017-10-13

反省

基本ドキュメントを読もう。

問題

require 'date'
require 'yaml'

Datetime のオブジェクトを YAML に変える

datetime = DateTime.now
=> #<DateTime: 2017-10-13T18:01:34+09:00((2458040j,32494s,84665000n),+32400s,2299161j)>
datetime.to_yaml
=> "--- !ruby/object:DateTime 2017-10-13 18:01:34.084665000 +09:00\n...\n"

YAML.load は出来る

YAML.load(datetime.to_yaml)
=> #<DateTime: 2017-10-13T18:01:34+09:00 ((2458040j,32494s,84665000n),+32400s,2299161j)>

YAML.safe_load が出来ない

YAML.safe_load(datetime.to_yaml)
Psych::DisallowedClass: Tried to load unspecified class: DateTime
from

環境

  • ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
2

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
0
0