6
6

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.

PStore

Last updated at Posted at 2013-03-19

PStoreを使うとRubyのオブジェクトを簡単に外部ファイルに保存することが出来る。

pstore.rb
# -*- coding: utf-8 -*-
require 'pstore'

# 引数に保存するファイル名を指定
db = PStore.new('pstore.db')

# ハッシュと同じようにキーを指定して保存
db.transaction do
  db['key_string'] = "hoge"
  db['key_array'] = ['fuga', 'poka']
end # ここで保存される

# transactionメソッドの引数にtrueを指定すると読み込みモード
db.transaction(true) do
  puts db['key_string']        # => hoge
  puts db['key_array']         # => fuga poka

  #root?メソッドでキーが存在するか確認
  puts db.root?('key_string')  # => true
  puts db.root?('ahaaha')      # => false

  #ループ
  db.roots.each { |key|
    puts "#{key}: #{db[key]}"
  }
end
$ ruby pstore.rb
hoge
fuga
poka
# root?
true
false
# root
key_string: hoge
key_array: ["hoge", "fuga"]

##参照
・久保秋真 著 作りながら学ぶRuby入門 第2版
PStore
標準添付ライブラリ紹介 【第 9 回】 PStore

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?