class YAML::DBM

Parent:
DBM

YAML + DBM = YDBM

YAML::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.

Public Instance Methods

ydbm[key] → value Show source
# File lib/yaml/dbm.rb, line 28
def []( key )
    fetch( key )
end

Return value associated with key from database.

Returns nil if there is no such key.

See fetch for more information.

ydbm[key] = value Show source
# File lib/yaml/dbm.rb, line 40
def []=( key, val )
    store( key, val )
end

Set key to value in database.

value will be converted to YAML before storage.

See store for more information.

delete(key) Show source
# File lib/yaml/dbm.rb, line 101
def delete( key )
    v = super( key )
    if String === v
        v = YAML.load( v )
    end
    v
end

Deletes value from database associated with key.

Returns value or nil.

Calls superclass method #delete
delete_if { |key, value| ... } Show source
# File lib/yaml/dbm.rb, line 116
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

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
each_pair { |key, value| ... } Show source
# File lib/yaml/dbm.rb, line 139
def each_pair # :yields: [key, value]
    keys.each { |k| yield k, fetch( k ) }
    self
end

Calls the given block once for each key, value pair in the database.

Returns self.

Also aliased as: each
each_value { |value| ... } Show source
# File lib/yaml/dbm.rb, line 150
def each_value # :yields: value
    super { |v| yield YAML.load( v ) }
    self
end

Calls the given block for each value in database.

Returns self.

Calls superclass method #each_value
fetch( key, ifnone = nil ) Show source
fetch( key ) { |key| ... }
# File lib/yaml/dbm.rb, line 55
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

Return value associated with key.

If there is no value for key and no block is given, returns ifnone.

Otherwise, calls block passing in the given key.

See DBM#fetch for more information.

Calls superclass method #fetch
has_value?(value) Show source
# File lib/yaml/dbm.rb, line 167
def has_value?( val )
    each_value { |v| return true if v == val }
    return false
end

Returns true if specified value is found in the database.

index( keystr ) Show source
# File lib/yaml/dbm.rb, line 75
def index( keystr )
    super( keystr.to_yaml )
end

Deprecated, used #key instead.

Note: #index makes warning from internal of ::DBM#index. It says 'DBM#index is deprecated; use #key', but #key behaves not same as #index.

Calls superclass method
invert → hash Show source
# File lib/yaml/dbm.rb, line 180
def invert
    h = {}
    keys.each { |k| h[ self.fetch( k ) ] = k }
    h
end

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 Show source
# File lib/yaml/dbm.rb, line 83
def key( keystr )
    invert[keystr]
end

Returns the key for the specified value.

reject { |key, value| ... } Show source
# File lib/yaml/dbm.rb, line 128
def reject
    hsh = self.to_hash
    hsh.reject { |k,v| yield k, v }
end

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) → ydbm Show source
# File lib/yaml/dbm.rb, line 192
def replace( hsh )
    clear
    update( hsh )
end

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.

select { |key, value| ... } Show source
select(*keys)
# File lib/yaml/dbm.rb, line 218
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

If a block is provided, returns a new array containing [key, value] pairs for which the block returns true.

Otherwise, same as values_at

shift → [key, value] Show source
# File lib/yaml/dbm.rb, line 204
def shift
    a = super
    a[1] = YAML.load( a[1] ) if a
    a
end

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.

Calls superclass method #shift
store(key, value) → value Show source
# File lib/yaml/dbm.rb, line 233
def store( key, val )
    super( key, val.to_yaml )
    val
end

Stores value in database with key as the index. value is converted to YAML before being stored.

Returns value

Calls superclass method #store
to_a → array Show source
# File lib/yaml/dbm.rb, line 258
def to_a
    a = []
    keys.each { |k| a.push [ k, self.fetch( k ) ] }
    a
end

Converts the contents of the database to an array of [key, value] arrays, and returns it.

to_hash → hash Show source
# File lib/yaml/dbm.rb, line 270
def to_hash
    h = {}
    keys.each { |k| h[ k ] = self.fetch( k ) }
    h
end

Converts the contents of the database to an in-memory Hash object, and returns it.

update(hash) → ydbm Show source
# File lib/yaml/dbm.rb, line 246
def update( hsh )
    hsh.each_pair do |k,v|
        self.store( k, v )
    end
    self
end

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 Show source
# File lib/yaml/dbm.rb, line 159
def values
    super.collect { |v| YAML.load( v ) }
end

Returns an array of values from the database.

Calls superclass method #values
values_at(*keys) Show source
# File lib/yaml/dbm.rb, line 91
def values_at( *keys )
    keys.collect { |k| fetch( k ) }
end

Returns an array containing the values associated with the given keys.

Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.