Method: PStore#fetch
- Defined in:
- lib/pstore.rb
#fetch(key, default = PStore::Error) ⇒ Object
Like #[], except that it accepts a default value for the store. If the key
does not exist:
-
Raises an exception if
default
isPStore::Error
. -
Returns the value of
default
otherwise:example_store do |store| store.transaction do store.fetch(:nope, nil) # => nil store.fetch(:nope) # Raises an exception. end end
Raises an exception if called outside a transaction block.
436 437 438 439 440 441 442 443 444 445 446 |
# File 'lib/pstore.rb', line 436 def fetch(key, default=PStore::Error) in_transaction unless @table.key? key if default == PStore::Error raise PStore::Error, format("undefined key '%s'", key) else return default end end @table[key] end |