In Files

  • yaml/dbm.rb

Class/Module Index [+]

Quicksearch

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

Constants

VERSION

Public Instance Methods

[]( key ) click to toggle source

Return value associated with key from database.

Returns nil if there is no such key.

 
               # File yaml/dbm.rb, line 23
def []( key )
    fetch( key )
end
            
[]=( key, value ) click to toggle source

Set key to value in database.

value will be converted to YAML before storage.

 
               # File yaml/dbm.rb, line 33
def []=( key, val )
    store( key, val )
end
            
delete( key ) click to toggle source

Deletes value from database associated with key.

Returns value or nil.

 
               # File yaml/dbm.rb, line 72
def delete( key )
    v = super( key )
    if String === v
        v = YAML.load( v )
    end
    v
end
            
delete_if() click to toggle source

Calls the given block once for each key, value pair in the database. Deletes all entries for which the block returns true.

Returns self.

 
               # File yaml/dbm.rb, line 84
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
            
each() click to toggle source
Alias for: each_pair
each_pair() click to toggle source

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

Returns self.

 
               # File yaml/dbm.rb, line 101
def each_pair # :yields: [key, value]
    keys.each { |k| yield k, fetch( k ) }
    self
end
            
Also aliased as: each
each_value() click to toggle source

Calls the given block for each value in database.

Returns self.

 
               # File yaml/dbm.rb, line 109
def each_value # :yields: value
    super { |v| yield YAML.load( v ) }
    self
end
            
fetch( key, ifnone = nil ) click to toggle source
fetch( key, &block )

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.

 
               # File yaml/dbm.rb, line 46
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?( val ) click to toggle source

Returns true if specified value is found in the database.

 
               # File yaml/dbm.rb, line 120
def has_value?( val )
    each_value { |v| return true if v == val }
    return false
end
            
index( keystr ) click to toggle source

Deprecated, used YAML::DBM#key instead.

 
               # File yaml/dbm.rb, line 60
def index( keystr )
    super( keystr.to_yaml )
end
            
invert() click to toggle source

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.

 
               # File yaml/dbm.rb, line 130
def invert
    h = {}
    keys.each { |k| h[ self.fetch( k ) ] = k }
    h
end
            
reject() click to toggle source

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 yaml/dbm.rb, line 93
def reject
    hsh = self.to_hash
    hsh.reject { |k,v| yield k, v }
end
            
replace( hsh ) click to toggle source

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 yaml/dbm.rb, line 139
def replace( hsh )
    clear
    update( hsh )
end
            
select( &block ) click to toggle source
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 yaml/dbm.rb, line 162
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() click to toggle source

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 yaml/dbm.rb, line 148
def shift
    a = super
    a[1] = YAML.load( a[1] ) if a
    a
end
            
store( key, value ) click to toggle source

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

Returns value

 
               # File yaml/dbm.rb, line 177
def store( key, val )
    super( key, val.to_yaml )
    val
end
            
to_a() click to toggle source

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

 
               # File yaml/dbm.rb, line 196
def to_a
    a = []
    keys.each { |k| a.push [ k, self.fetch( k ) ] }
    a
end
            
to_hash() click to toggle source

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

 
               # File yaml/dbm.rb, line 205
def to_hash
    h = {}
    keys.each { |k| h[ k ] = self.fetch( k ) }
    h
end
            
update( hsh ) click to toggle source

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.

 
               # File yaml/dbm.rb, line 187
def update( hsh )
    hsh.keys.each do |k|
        self.store( k, hsh.fetch( k ) )
    end
    self
end
            
values() click to toggle source

Returns an array of values from the database.

 
               # File yaml/dbm.rb, line 115
def values
    super.collect { |v| YAML.load( v ) }
end
            
values_at( *keys ) click to toggle source

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

 
               # File yaml/dbm.rb, line 65
def values_at( *keys )
    keys.collect { |k| fetch( k ) }
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus