Class: YAML::DBM
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
DBM
|
|
Instance Chain:
self,
DBM
|
|
Inherits: |
DBM
|
Defined in: | lib/yaml/dbm.rb |
Overview
::YAML + DBM
= YDBM
DBM
provides the same interface as ::DBM
.
However, while DBM
only allows strings for both keys and values, this library allows one to use most Ruby objects for values by first converting them to ::YAML. Keys must be strings.
Conversion to and from ::YAML is performed automatically.
See the documentation for ::DBM
and ::YAML for more information.
Constant Summary
-
VERSION =
Internal use only
# File 'lib/yaml/dbm.rb', line 19"0.1"
Instance Method Summary
-
#[](key) ⇒ value
Return value associated with #key from database.
-
#[]=(key, value)
Set #key to
value
in database. -
#delete(key)
Deletes value from database associated with #key.
-
#delete_if {|key, value| ... }
Calls the given block once for each #key,
value
pair in the database. -
#each
Alias for #each_pair.
-
#each_pair {|key, value| ... }
(also: #each)
Calls the given block once for each #key,
value
pair in the database. -
#each_value {|value| ... }
Calls the given block for each value in database.
-
#fetch(key, ifnone = nil)
Return value associated with #key.
-
#has_value?(value) ⇒ Boolean
Returns true if specified
value
is found in the database. -
#index(keystr)
Deprecated, used #key instead.
-
#invert ⇒ Hash
Returns a Hash (not a
DBM
database) created by using each value in the database as a key, with the corresponding key as its value. -
#key(value) ⇒ String
Returns the key for the specified value.
-
#reject {|key, value| ... }
Converts the contents of the database to an in-memory Hash, then calls
Hash#reject
with the specified code block, returning a new Hash. -
#replace(hash) ⇒ DBM
Replaces the contents of the database with the contents of the specified object.
-
#select {|key, value| ... }
If a block is provided, returns a new array containing [key, value] pairs for which the block returns true.
-
#shift ⇒ Array, value
Removes a [key, value] pair from the database, and returns it.
-
#store(key, value) ⇒ value
Stores
value
in database with #key as the index. -
#to_a ⇒ Array
Converts the contents of the database to an array of [key, value] arrays, and returns it.
-
#to_hash ⇒ Hash
Converts the contents of the database to an in-memory Hash object, and returns it.
-
#update(hash) ⇒ DBM
Updates the database with multiple values from the specified object.
-
#values
Returns an array of values from the database.
-
#values_at(*keys)
Returns an array containing the values associated with the given keys.
Instance Method Details
#[](key) ⇒ value
#[]=(key, value)
#delete(key)
Deletes value from database associated with #key.
Returns value or nil
.
#delete_if {|key, value| ... }
Calls the given block once for each #key, value
pair in the database. Deletes all entries for which the block returns true.
Returns self
.
#each
Alias for #each_pair.
# File 'lib/yaml/dbm.rb', line 277
alias :each :each_pair
#each_pair {|key, value| ... } Also known as: #each
Calls the given block once for each #key, value
pair in the database.
Returns self
.
#each_value {|value| ... }
Calls the given block for each value in database.
Returns self
.
# File 'lib/yaml/dbm.rb', line 151
def each_value # :yields: value super { |v| yield YAML.load( v ) } self end
#fetch(key, ifnone = nil)
#fetch(key) {|key| ... }
# File 'lib/yaml/dbm.rb', line 56
def fetch( keystr, ifnone = nil ) begin val = super( keystr ) return YAML.load( val ) if String === val rescue IndexError end if block_given? yield keystr else ifnone end end
#has_value?(value) ⇒ Boolean
Returns true if specified value
is found in the database.
# File 'lib/yaml/dbm.rb', line 168
def has_value?( val ) each_value { |v| return true if v == val } return false end
#index(keystr)
# File 'lib/yaml/dbm.rb', line 76
def index( keystr ) super( keystr.to_yaml ) end
#invert ⇒ Hash
Returns a Hash (not a DBM
database) created by using each value in the database as a key, with the corresponding key as its value.
Note that all values in the hash will be Strings, but the keys will be actual objects.
#key(value) ⇒ String
Returns the key for the specified value.
# File 'lib/yaml/dbm.rb', line 84
def key( keystr ) invert[keystr] end
#reject {|key, value| ... }
Converts the contents of the database to an in-memory Hash, then calls Hash#reject
with the specified code block, returning a new Hash.
# File 'lib/yaml/dbm.rb', line 129
def reject hsh = self.to_hash hsh.reject { |k,v| yield k, v } end
#replace(hash) ⇒ DBM
Replaces the contents of the database with the contents of the specified object. Takes any object which implements the each_pair method, including Hash and DBM
objects.
# File 'lib/yaml/dbm.rb', line 193
def replace( hsh ) clear update( hsh ) end
#select {|key, value| ... }
#select(*keys)
If a block is provided, returns a new array containing [key, value] pairs for which the block returns true.
Otherwise, same as #values_at
# File 'lib/yaml/dbm.rb', line 219
def select( *keys ) if block_given? self.keys.collect { |k| v = self[k]; [k, v] if yield k, v }.compact else values_at( *keys ) end end
#shift ⇒ Array
, value
Removes a [key, value] pair from the database, and returns it. If the database is empty, returns nil
.
The order in which values are removed/returned is not guaranteed.
# File 'lib/yaml/dbm.rb', line 205
def shift a = super a[1] = YAML.load( a[1] ) if a a end
#store(key, value) ⇒ value
#to_a ⇒ Array
Converts the contents of the database to an array of [key, value] arrays, and returns it.
#to_hash ⇒ Hash
Converts the contents of the database to an in-memory Hash object, and returns it.
#update(hash) ⇒ DBM
Updates the database with multiple values from the specified object. Takes any object which implements the each_pair method, including Hash and DBM
objects.
Returns self
.
#values
Returns an array of values from the database.
# File 'lib/yaml/dbm.rb', line 160
def values super.collect { |v| YAML.load( v ) } end
#values_at(*keys)
Returns an array containing the values associated with the given keys.
# File 'lib/yaml/dbm.rb', line 92
def values_at( *keys ) keys.collect { |k| fetch( k ) } end