Class: Hashery::OrderedHash

### Home Classes Methods

### In Files

- lib/hashery/ordered_hash.rb

### Parent

Hash

### Methods

- ::[]
- ::new
- #[]=
- #clear
- #delete
- #delete_if
- #each
- #each_key
- #each_value
- #initialize_copy
- #inspect
- #keys
- #merge
- #merge!
- #reject
- #reject!
- #replace
- #shift
- #sync_keys!
- #to_a
- #to_hash
- #to_yaml
- #to_yaml_type
- #values

### Files

- LICENSE.txt
- NOTICE.txt

### Class/Module Index

Quicksearch

- Hashery
- Hashery::Association
- Hashery::Association::Kernel
- Hashery::CRUDHash
- Hashery::CastingHash
- Hashery::Dictionary
- Hashery::FuzzyHash
- Hashery::IniHash
- Hashery::KeyHash
- Hashery::LRUHash
- Hashery::LinkedList
- Hashery::LinkedList::Node
- Hashery::OpenCascade
- Hashery::OpenHash
- Hashery::OrderedHash
- Hashery::PathHash
- Hashery::PropertyHash
- Hashery::QueryHash
- Hashery::Stash
- Hashery::StaticHash
- Hash

No matching classes.

# Hashery::OrderedHash

OrderedHash is a simple ordered hash implmentation, for users of Ruby 1.8.7 or less.

NOTE: As of Ruby 1.9+ this class is not needed, since Ruby 1.9's standard Hash tracks inseration order.

This implementation derives from the same class in ActiveSupport library.

### Public Class Methods

[](*args) click to toggle source

    # File lib/hashery/ordered_hash.rb, line 36
    def self.[](*args)
      ordered_hash = new

      if (args.length == 1 && args.first.is_a?(Array))
        args.first.each do |key_value_pair|
          next unless (key_value_pair.is_a?(Array))
          ordered_hash[key_value_pair[0]] = key_value_pair[1]
        end

        return ordered_hash
      end

      unless (args.size % 2 == 0)
        raise ArgumentError.new("odd number of arguments for Hash")
      end

      args.each_with_index do |val, ind|
        next if (ind % 2 != 0)
        ordered_hash[val] = args[ind + 1]
      end

      ordered_hash
    end

new(*args, &block) click to toggle source

    # File lib/hashery/ordered_hash.rb, line 31
    def initialize(*args, &block)
      super
      @keys = []
    end

### Public Instance Methods

[]=(key, value) click to toggle source

    # File lib/hashery/ordered_hash.rb, line 65
    def []=(key, value)
      @keys << key unless key?(key)
      super(key, value)
    end

clear() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 124
    def clear
      super
      @keys.clear
      self
    end

delete(key) click to toggle source

    # File lib/hashery/ordered_hash.rb, line 70
    def delete(key)
      if has_key? key
        index = @keys.index(key)
        @keys.delete_at(index)
      end
      super(key)
    end

delete_if() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 78
    def delete_if
      super
      sync_keys!
      self
    end

each() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 118
    def each
      @keys.each{ |key| yield(key, self[key]) }
    end

each_key() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 110
    def each_key
      @keys.each{ |key| yield(key) }
    end

each_value() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 114
    def each_value
      @keys.each{ |key| yield(self[key]) }
    end

initialize_copy(other) click to toggle source

    # File lib/hashery/ordered_hash.rb, line 60
    def initialize_copy(other)
      super(other)
      @keys = other.keys
    end

inspect() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 153
    def inspect
      "#<OrderedHash #{super}>"
    end

keys() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 94
    def keys
      @keys.dup
    end

merge(other_hash) click to toggle source

    # File lib/hashery/ordered_hash.rb, line 141
    def merge(other_hash)
      dup.merge!(other_hash)
    end

merge!(other_hash) click to toggle source

    # File lib/hashery/ordered_hash.rb, line 136
    def merge!(other_hash)
      other_hash.each{ |k,v| self[k] = v }
      self
    end

reject(&block) click to toggle source

    # File lib/hashery/ordered_hash.rb, line 90
    def reject(&block)
      dup.reject!(&block)
    end

reject!() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 84
    def reject!
      super
      sync_keys!
      self
    end

replace(other) click to toggle source

When replacing with another hash, the initial order of our keys must come from the other hash, ordered or not.

    # File lib/hashery/ordered_hash.rb, line 147
    def replace(other)
      super
      @keys = other.keys
      self
    end

shift() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 130
    def shift
      k = @keys.first
      v = delete(k)
      [k, v]
    end

sync_keys!() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 159
    def sync_keys!
      @keys.delete_if{ |k| !key?(k) }
    end

to_a() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 106
    def to_a
      @keys.map{ |key| [ key, self[key] ] }
    end

to_hash() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 102
    def to_hash
      self
    end

to_yaml(opts = {}) click to toggle source

    # File lib/hashery/ordered_hash.rb, line 18
    def to_yaml(opts = {})
      YAML.quick_emit(self, opts) do |out|
        out.seq(taguri, to_yaml_style) do |seq|
          each do |k, v|
            seq.add(k => v)
          end
        end
      end
    end

to_yaml_type() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 14
    def to_yaml_type
      "!tag:yaml.org,2002:omap"
    end

values() click to toggle source

    # File lib/hashery/ordered_hash.rb, line 98
    def values
      @keys.collect{ |key| self[key] }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.