Module OHashMixIn
In: lib/o_hash.rb

AUTHOR

   jan molic /mig/at/1984/dot/cz/
   updated by: Nicolas Pouillard  <ertai@lrde.epita.fr>

DESCRIPTION

   Override hash to make it creation order preserved.
   This will be obsolete - it is better to use special OHash class
   than overriding.
   Public domain.

THANKS

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

$LastChangedBy$ $Id: /w/fey/ruby_ex/trunk/lib/o_hash.rb 24384 2006-07-09T16:59:08.734236Z ertai $

Methods

==   []=   clear   delete   delete_if   each   each_key   each_pair   each_value   inspect   invert   keys   merge   merge!   ordered?   pretty_print   pretty_print_cycle   reject   reject!   replace   select   shift   store   to_a   to_s   update   values  

Public Instance methods

[Source]

# File lib/o_hash.rb, line 31
  def == ( hsh2 )
    return false if !hsh2.respond_to? :order or @order != hsh2.order
    super hsh2
  end

[Source]

# File lib/o_hash.rb, line 26
  def []= ( a, b )
    @order.push a unless has_key? a
    super a,b
  end

[Source]

# File lib/o_hash.rb, line 36
  def clear
    @order = []
    super
  end

[Source]

# File lib/o_hash.rb, line 41
  def delete ( key )
    @order.delete key
    super
  end

[Source]

# File lib/o_hash.rb, line 63
  def delete_if
    @order.clone.each do |k|
      delete k if yield(k, self[k])
    end
    self
  end

[Source]

# File lib/o_hash.rb, line 56
  def each
    @order.each { |k| yield(k, self[k]) }
    self
  end

[Source]

# File lib/o_hash.rb, line 46
  def each_key
    @order.each { |k| yield k }
    self
  end
each_pair()

Alias for each

[Source]

# File lib/o_hash.rb, line 51
  def each_value
    @order.each { |k| yield self[k] }
    self
  end

[Source]

# File lib/o_hash.rb, line 115
  def inspect
    arr = to_a.map{ |k, v| "#{k.inspect}=>#{v.inspect}" }
    '{(ordered) ' + arr.join(', ') + ' }'
  end

[Source]

# File lib/o_hash.rb, line 80
  def invert
    hsh2 = self.class.new
    @order.each { |k| hsh2[self[k]] = k }
    hsh2
  end

[Source]

# File lib/o_hash.rb, line 76
  def keys
    @order
  end

[Source]

# File lib/o_hash.rb, line 149
  def merge ( hsh2 )
    self.dup.update(hsh2)
  end

[Source]

# File lib/o_hash.rb, line 144
  def merge! ( hsh2 )
    hsh2.each { |k,v| self[k] = v }
    self
  end

[Source]

# File lib/o_hash.rb, line 159
  def ordered?
    !@order.nil?
  end

[Source]

# File lib/o_hash.rb, line 120
  def pretty_print ( q )
    q.group(1, '{(ordered) ', '}') {
      q.seplist(self, nil, :each_pair) {|k, v|
        q.group {
          q.pp k
          q.text '=>'
          q.group(1) {
            q.breakable ''
            q.pp v
          }
        }
      }
    }
  end

[Source]

# File lib/o_hash.rb, line 135
  def pretty_print_cycle(q)
    q.text(empty? ? '{(ordered)}' : '{(ordered) ...}')
  end

[Source]

# File lib/o_hash.rb, line 86
  def reject ( &block )
    self.dup.delete_if(&block)
  end

[Source]

# File lib/o_hash.rb, line 90
  def reject! ( &block )
    hsh2 = reject(&block)
    self == hsh2 ? nil : hsh2
  end

[Source]

# File lib/o_hash.rb, line 95
  def replace ( hsh2 )
    @order = hsh2.keys 
    super hsh2
  end

[Source]

# File lib/o_hash.rb, line 153
  def select
    ary = []
    each { |k,v| ary << [k,v] if yield k,v }
    ary
  end

[Source]

# File lib/o_hash.rb, line 100
  def shift
    key = @order.first
    key ? [key,delete(key)] : super
  end

[Source]

# File lib/o_hash.rb, line 21
  def store ( a, b )
    @order.push a unless has_key? a
    super a,b
  end

[Source]

# File lib/o_hash.rb, line 105
  def to_a
    ary = []
    each { |k,v| ary << [k,v] }
    ary
  end

[Source]

# File lib/o_hash.rb, line 111
  def to_s
    to_a.to_s
  end

[Source]

# File lib/o_hash.rb, line 139
  def update ( hsh2 )
    hsh2.each { |k,v| self[k] = v }
    self
  end

[Source]

# File lib/o_hash.rb, line 70
  def values
    ary = []
    @order.each { |k| ary << self[k] }
    ary
  end

[Validate]