class Storable::OrderedHash

AUTHOR

jan molic /mig/at/1984/dot/cz/

DESCRIPTION

Hash with preserved order and some array-like extensions
Public domain.

THANKS

Andrew Johnson for his suggestions and fixes of Hash[],
merge, to_a, inspect and shift

Attributes

order[RW]
to_yaml_style[RW]

Public Class Methods

[](*args) click to toggle source
# File lib/storable/orderedhash.rb, line 15
def [] *args
  hsh = Storable::OrderedHash.new
  if Hash === args[0]
    hsh.replace args[0]
  elsif (args.size % 2) != 0
    raise ArgumentError, "odd number of elements for Hash"
  else
    0.step(args.size - 1, 2) do |a|
      b = a + 1
      hsh[args[a]] = args[b]
    end
  end
  hsh
end
new(*a, &b) click to toggle source
Calls superclass method
# File lib/storable/orderedhash.rb, line 30
def initialize(*a, &b)
  super
  @order = []
end
to_yaml(opts = {}) click to toggle source
Calls superclass method
# File lib/storable/orderedhash.rb, line 178
def to_yaml opts = {}
  begin
    @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
  rescue
    @to_yaml_style = :inline
    super
  end
end

Public Instance Methods

==(hsh2) click to toggle source
Calls superclass method
# File lib/storable/orderedhash.rb, line 43
def == hsh2
    return false if @order != hsh2.order
    super hsh2
end
[]=(a,b)
Alias for: store
__class__() click to toggle source
# File lib/storable/orderedhash.rb, line 161
def __class__
  Storable::OrderedHash
end
class() click to toggle source
# File lib/storable/orderedhash.rb, line 158
def class
  Hash
end
clear() click to toggle source
Calls superclass method
# File lib/storable/orderedhash.rb, line 47
def clear
    @order = []
    super
end
delete(key) click to toggle source
Calls superclass method
# File lib/storable/orderedhash.rb, line 51
def delete key
    @order.delete key
    super
end
delete_if() { |k| ... } click to toggle source
# File lib/storable/orderedhash.rb, line 68
def delete_if
    @order.clone.each { |k| 
        delete k if yield(k)
    }
    self
end
each() { |k,self| ... } click to toggle source
# File lib/storable/orderedhash.rb, line 63
def each
    @order.each { |k| yield k,self[k] }
    self
end
Also aliased as: each_pair
each_key() { |k| ... } click to toggle source
# File lib/storable/orderedhash.rb, line 55
def each_key
    @order.each { |k| yield k }
    self
end
each_pair()
Alias for: each
each_value() { |self| ... } click to toggle source
# File lib/storable/orderedhash.rb, line 59
def each_value
    @order.each { |k| yield self[k] }
    self
end
each_with_index() { |k, self, index| ... } click to toggle source
# File lib/storable/orderedhash.rb, line 193
def each_with_index
  @order.each_with_index { |k, index| yield k, self[k], index }
  self
end
first() click to toggle source
# File lib/storable/orderedhash.rb, line 82
def first
  {@order.first => self[@order.first]}
end
inspect() click to toggle source
# File lib/storable/orderedhash.rb, line 138
def inspect
    ary = []
    each {|k,v| ary << k.inspect + "=>" + v.inspect}
    '{' + ary.join(", ") + '}'
end
invert() click to toggle source
# File lib/storable/orderedhash.rb, line 88
def invert
    hsh2 = Hash.new    
    @order.each { |k| hsh2[self[k]] = k }
    hsh2
end
keys() click to toggle source
# File lib/storable/orderedhash.rb, line 79
def keys
    @order
end
last() click to toggle source
# File lib/storable/orderedhash.rb, line 85
def last
  {@order.last => self[@order.last]}
end
merge(hsh2) click to toggle source
# File lib/storable/orderedhash.rb, line 148
def merge hsh2
    ##self.dup update(hsh2)   ## 2009-05-12 -- delano
    update hsh2               ## dup doesn't take an argument
                              ## and there's no need for it here
end
merge!(hsh2)
Alias for: update
orig_store(a,b)
Alias for: store
pop() click to toggle source
# File lib/storable/orderedhash.rb, line 126
def pop
    key = @order.last
    key ? [key,delete(key)] : nil
end
push(k,v) click to toggle source
# File lib/storable/orderedhash.rb, line 117
def push k,v
    unless self.include? k
        @order.push k
        orig_store(k,v)
        true
    else
        false
    end
end
reject(&block) click to toggle source
# File lib/storable/orderedhash.rb, line 93
def reject &block
    self.dup.delete_if( &block)
end
reject!(&block) click to toggle source
# File lib/storable/orderedhash.rb, line 96
def reject! &block
    hsh2 = reject( &block)
    self == hsh2 ? nil : hsh2
end
replace(hsh2) click to toggle source
Calls superclass method
# File lib/storable/orderedhash.rb, line 100
def replace hsh2
    @order = hsh2.keys 
    super hsh2
end
select() { |k,v| ... } click to toggle source
# File lib/storable/orderedhash.rb, line 153
def select
    ary = []
    each { |k,v| ary << [k,v] if yield k,v }
    ary
end
shift() click to toggle source
Calls superclass method
# File lib/storable/orderedhash.rb, line 104
def shift
    key = @order.first
    key ? [key,delete(key)] : super
end
store(a,b) click to toggle source
Calls superclass method
# File lib/storable/orderedhash.rb, line 38
def store a,b
    @order.push a unless has_key? a
    super a,b
end
Also aliased as: orig_store, []=
store_only(a,b) click to toggle source
# File lib/storable/orderedhash.rb, line 34
def store_only a,b
    store a,b
end
to_a() click to toggle source
# File lib/storable/orderedhash.rb, line 130
def to_a
    ary = []
    each { |k,v| ary << [k,v] }
    ary
end
to_s() click to toggle source
# File lib/storable/orderedhash.rb, line 135
def to_s
    self.to_a.to_s
end
unshift(k,v) click to toggle source
# File lib/storable/orderedhash.rb, line 108
def unshift k,v
    unless self.include? k
        @order.unshift k
        orig_store(k,v)
        true
    else
        false
    end
end
update(hsh2) click to toggle source
# File lib/storable/orderedhash.rb, line 143
def update hsh2
    hsh2.each { |k,v| self[k] = v }
    self
end
Also aliased as: merge!
values() click to toggle source
# File lib/storable/orderedhash.rb, line 74
def values
    ary = []
    @order.each { |k| ary.push self[k] }
    ary
end
yaml_inline!() click to toggle source
# File lib/storable/orderedhash.rb, line 191
def yaml_inline!() self.yaml_inline = true end
yaml_inline=(bool) click to toggle source
# File lib/storable/orderedhash.rb, line 166
def yaml_inline= bool
  if respond_to?("to_yaml_style")
    self.to_yaml_style = :inline
  else
    unless defined? @__yaml_inline_meth
      @__yaml_inline_meth =
        lambda {|opts|
          YAML::quick_emit(object_id, opts) {|emitter|
            emitter << '{ ' << map{|kv| kv.join ': '}.join(', ') << ' }'
          }
        }
      class << self
        def to_yaml opts = {}
          begin
            @__yaml_inline ? @__yaml_inline_meth[ opts ] : super
          rescue
            @to_yaml_style = :inline
            super
          end
        end
      end
    end
  end
  @__yaml_inline = bool
end