class YAML::DBM
See the documentation for ::DBM and ::YAML for more information.
Conversion to and from YAML is performed automatically.
by first converting them to YAML. Keys must be strings.
this library allows one to use most Ruby objects for values
However, while DBM only allows strings for both keys and values,
YAML::DBM provides the same interface as ::DBM.
YAML + DBM = YDBM
def []( key )
Returns +nil+ if there is no such +key+.
Return value associated with +key+ from database.
ydbm[key] -> value
:call-seq:
def []( key ) fetch( key ) end
def []=( key, val )
+value+ will be converted to YAML before storage.
Set +key+ to +value+ in database.
ydbm[key] = value
:call-seq:
def []=( key, val ) store( key, val ) end
def delete( key )
Deletes value from database associated with +key+.
ydbm.delete(key)
:call-seq:
def delete( key ) v = super( key ) if String === v if YAML.respond_to?(:safe_load) v = YAML.safe_load( v ) else v = YAML.load( v ) end end v end
def delete_if # :yields: [key, value]
Returns +self+.
Deletes all entries for which the block returns true.
Calls the given block once for each +key+, +value+ pair in the database.
ydbm.delete_if { |key, value| ... }
:call-seq:
def delete_if # :yields: [key, value] del_keys = keys.dup del_keys.delete_if { |k| yield( k, fetch( k ) ) == false } del_keys.each { |k| delete( k ) } self end
def each_pair # :yields: [key, value]
Returns +self+.
Calls the given block once for each +key+, +value+ pair in the database.
ydbm.each_pair { |key, value| ... }
:call-seq:
def each_pair # :yields: [key, value] keys.each { |k| yield k, fetch( k ) } self end
def each_value # :yields: value
Returns +self+.
Calls the given block for each value in database.
ydbm.each_value { |value| ... }
:call-seq:
def each_value # :yields: value super { |v| yield YAML.respond_to?(:safe_load) ? YAML.safe_load( v ) : YAML.load( v ) } self end
def fetch( keystr, ifnone = nil )
Otherwise, calls block passing in the given +key+.
If there is no value for +key+ and no block is given, returns +ifnone+.
Return value associated with +key+.
ydbm.fetch( key ) { |key| ... }
ydbm.fetch( key, ifnone = nil )
:call-seq:
def fetch( keystr, ifnone = nil ) begin val = super( keystr ) if String === val if YAML.respond_to?(:safe_load) return YAML.safe_load( val ) else return YAML.load( val ) end end rescue IndexError end if block_given? yield keystr else ifnone end end
def has_value?( val )
ydbm.has_value?(value)
:call-seq:
def has_value?( val ) each_value { |v| return true if v == val } return false end
def index( keystr )
behaves not same as DBM#index.
It says 'DBM#index is deprecated; use DBM#key', but DBM#key
YAML::DBM#index makes warning from internal of ::DBM#index.
Note:
----
Deprecated, used YAML::DBM#key instead.
def index( keystr ) super( keystr.to_yaml ) end
def invert
Note that all values in the hash will be Strings, but the keys will be
database as a key, with the corresponding key as its value.
Returns a Hash (not a DBM database) created by using each value in the
ydbm.invert -> hash
:call-seq:
def invert h = {} keys.each { |k| h[ self.fetch( k ) ] = k } h end
def key( keystr )
ydbm.key(value) -> string
:call-seq:
def key( keystr ) invert[keystr] end
def reject
Converts the contents of the database to an in-memory Hash, then calls
ydbm.reject { |key, value| ... }
:call-seq:
def reject hsh = self.to_hash hsh.reject { |k,v| yield k, v } end
def replace( hsh )
object. Takes any object which implements the each_pair method, including
Replaces the contents of the database with the contents of the specified
ydbm.replace(hash) -> ydbm
:call-seq:
def replace( hsh ) clear update( hsh ) end
def select( *keys )
for which the block returns true.
If a block is provided, returns a new array containing [key, value] pairs
ydbm.select(*keys)
ydbm.select { |key, value| ... }
:call-seq:
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
def shift
If the database is empty, returns +nil+.
Removes a [key, value] pair from the database, and returns it.
ydbm.shift -> [key, value]
:call-seq:
def shift a = super if a a[1] = YAML.respond_to?(:safe_load) ? YAML.safe_load( a[1] ) : YAML.load( a[1] ) end a end
def store( key, val )
to YAML before being stored.
Stores +value+ in database with +key+ as the index. +value+ is converted
ydbm.store(key, value) -> value
:call-seq:
def store( key, val ) super( key, val.to_yaml ) val end
def to_a
Converts the contents of the database to an array of [key, value] arrays,
ydbm.to_a -> array
:call-seq:
def to_a a = [] keys.each { |k| a.push [ k, self.fetch( k ) ] } a end
def to_hash
Converts the contents of the database to an in-memory Hash object, and
ydbm.to_hash -> hash
:call-seq:
def to_hash h = {} keys.each { |k| h[ k ] = self.fetch( k ) } h end
def update( hsh )
Hash and DBM objects.
Takes any object which implements the each_pair method, including
Updates the database with multiple values from the specified object.
ydbm.update(hash) -> ydbm
:call-seq:
def update( hsh ) hsh.each_pair do |k,v| self.store( k, v ) end self end
def values
ydbm.values
:call-seq:
def values super.collect { |v| YAML.respond_to?(:safe_load) ? YAML.safe_load( v ) : YAML.load( v ) } end
def values_at( *keys )
ydbm.values_at(*keys)
:call-seq:
def values_at( *keys ) keys.collect { |k| fetch( k ) } end