開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Ruby 2.3 (プログラミング言語)
Head First Ruby (Jay McGavren (著)、O'Reilly Media)のChapter 15.(Saving and Loading Data: Keep It Around)、Saving objects to a file with YAML::Store、CODE MAGNETS(No. 7811)を取り組んでみる。
CODE MAGNETS(No. 7811)
コード(Emacs)
#!/usr/bin/env ruby2.3
# -*- coding: utf-8 -*
require 'yaml/store'
class Book
attr_accessor :title, :pages
end
book = Book.new
book.title = 'Head First Ruby'
book.pages = 450
store = YAML::Store.new('books.yml')
store.transaction do
store['HFRB'] = book
end
入出力結果(Terminal)
$ ./sample1.rb $ cat books.yml --- HFRB: !ruby/object:Book title: Head First Ruby pages: 450 $
0 コメント:
コメントを投稿