123456789_123456789_123456789_123456789_123456789_

Class: YAML::DBM

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, DBM
Instance Chain:
self, DBM
Inherits: DBM
  • Object
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.

Instance Method Summary

Instance Method Details

#[](key) ⇒ value

Return value associated with #key from database.

Returns nil if there is no such #key.

See #fetch for more information.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 32

def []( key )
    fetch( key )
end

#[]=(key, value)

Set #key to value in database.

value will be converted to ::YAML before storage.

See #store for more information.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 44

def []=( key, val )
    store( key, val )
end

#delete(key)

Deletes value from database associated with #key.

Returns value or nil.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 111

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

#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.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 130

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

Alias for #each_pair.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 292

alias :each :each_pair

#each_pair {|key, value| ... } Also known as: #each, #each

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

Returns self.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 153

def each_pair # :yields: [key, value]
    keys.each { |k| yield k, fetch( k ) }
    self
end

#each_value {|value| ... }

Calls the given block for each value in database.

Returns self.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 164

def each_value # :yields: value
    super { |v| yield YAML.respond_to?(:safe_load) ? YAML.safe_load( v ) : YAML.load( v ) }
    self
end

#fetch(key, ifnone = nil) #fetch(key) {|key| ... }

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.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 59

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

#has_value?(value) ⇒ Boolean

Returns true if specified value is found in the database.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 181

def has_value?( val )
    each_value { |v| return true if v == val }
    return false
end

#index(keystr)

Deprecated, used #key instead.


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

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 85

def index( keystr )
    super( keystr.to_yaml )
end

#invertHash

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.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 194

def invert
    h = {}
    keys.each { |k| h[ self.fetch( k ) ] = k }
    h
end

#key(value) ⇒ String

Returns the key for the specified value.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 93

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.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 142

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.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 206

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

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 234

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

#shiftArray, 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.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 218

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

#store(key, value) ⇒ value

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

Returns value

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 249

def store( key, val )
    super( key, val.to_yaml )
    val
end

#to_aArray

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

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 274

def to_a
    a = []
    keys.each { |k| a.push [ k, self.fetch( k ) ] }
    a
end

#to_hashHash

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

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 286

def to_hash
    h = {}
    keys.each { |k| h[ k ] = self.fetch( k ) }
    h
end

#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.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 262

def update( hsh )
    hsh.each_pair do |k,v|
        self.store( k, v )
    end
    self
end

#values

Returns an array of values from the database.

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 173

def values
    super.collect { |v| YAML.respond_to?(:safe_load) ? YAML.safe_load( v ) : YAML.load( v ) }
end

#values_at(*keys)

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

[ GitHub ]

  
# File 'lib/yaml/dbm.rb', line 101

def values_at( *keys )
    keys.collect { |k| fetch( k ) }
end