Class: ActiveSupport::OrderedHash

### Home Classes Methods

### In Files

- lib/active_support/ordered_hash.rb

### Parent

Hash

### Methods

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

### Class/Module Index

Quicksearch

- ActiveSupport
- ActiveSupport::Autoload
- ActiveSupport::BacktraceCleaner
- ActiveSupport::Base64
- ActiveSupport::BasicObject
- ActiveSupport::Benchmarkable
- ActiveSupport::BufferedLogger
- ActiveSupport::BufferedLogger::Severity
- ActiveSupport::Cache
- ActiveSupport::Cache::Entry
- ActiveSupport::Cache::FileStore
- ActiveSupport::Cache::MemCacheStore
- ActiveSupport::Cache::MemoryStore
- ActiveSupport::Cache::NullStore
- ActiveSupport::Cache::Store
- ActiveSupport::Cache::Strategy
- ActiveSupport::Cache::Strategy::LocalCache
- ActiveSupport::Cache::Strategy::LocalCache::LocalStore
- ActiveSupport::Callbacks
- ActiveSupport::Callbacks::ClassMethods
- ActiveSupport::Concern
- ActiveSupport::Configurable
- ActiveSupport::Configurable::ClassMethods
- ActiveSupport::Configurable::Configuration
- ActiveSupport::Dependencies
- ActiveSupport::Dependencies::ClassCache
- ActiveSupport::Dependencies::WatchStack
- ActiveSupport::Deprecation
- ActiveSupport::DescendantsTracker
- ActiveSupport::Duration
- ActiveSupport::FileUpdateChecker
- ActiveSupport::FileWatcher
- ActiveSupport::FileWatcher::Backend
- ActiveSupport::Gzip
- ActiveSupport::Gzip::Stream
- ActiveSupport::HashWithIndifferentAccess
- ActiveSupport::HashWithIndifferentAccess
- ActiveSupport::Inflector
- ActiveSupport::Inflector::Inflections
- ActiveSupport::InheritableOptions
- ActiveSupport::JSON
- ActiveSupport::JSON::Encoding
- ActiveSupport::JSON::Encoding::CircularReferenceError
- ActiveSupport::JSON::Encoding::Encoder
- ActiveSupport::JSON::Variable
- ActiveSupport::LogSubscriber
- ActiveSupport::LogSubscriber::TestHelper
- ActiveSupport::LogSubscriber::TestHelper::MockLogger
- ActiveSupport::Memoizable
- ActiveSupport::Memoizable::InstanceMethods
- ActiveSupport::MessageEncryptor
- ActiveSupport::MessageEncryptor::InvalidMessage
- ActiveSupport::MessageVerifier
- ActiveSupport::MessageVerifier::InvalidSignature
- ActiveSupport::Multibyte
- ActiveSupport::Multibyte::Chars
- ActiveSupport::Multibyte::EncodingError
- ActiveSupport::Multibyte::Unicode
- ActiveSupport::Multibyte::Unicode::Codepoint
- ActiveSupport::Multibyte::Unicode::UnicodeDatabase
- ActiveSupport::Notifications
- ActiveSupport::Notifications::Event
- ActiveSupport::Notifications::Fanout
- ActiveSupport::Notifications::Instrumenter
- ActiveSupport::OrderedHash
- ActiveSupport::OrderedOptions
- ActiveSupport::Railtie
- ActiveSupport::Rescuable
- ActiveSupport::Rescuable::ClassMethods
- ActiveSupport::SafeBuffer
- ActiveSupport::SafeBuffer::SafeConcatError
- ActiveSupport::StringInquirer
- ActiveSupport::TaggedLogging
- ActiveSupport::TestCase
- ActiveSupport::Testing
- ActiveSupport::Testing::Assertions
- ActiveSupport::Testing::Declarative
- ActiveSupport::Testing::Isolation
- ActiveSupport::Testing::Isolation::Forking
- ActiveSupport::Testing::Isolation::MiniTest
- ActiveSupport::Testing::Isolation::Subprocess
- ActiveSupport::Testing::Isolation::TestUnit
- ActiveSupport::Testing::Pending
- ActiveSupport::Testing::Performance
- ActiveSupport::Testing::Performance::Benchmarker
- ActiveSupport::Testing::Performance::ForClassicTestUnit
- ActiveSupport::Testing::Performance::ForMiniTest
- ActiveSupport::Testing::Performance::Metrics
- ActiveSupport::Testing::Performance::Metrics::Amount
- ActiveSupport::Testing::Performance::Metrics::Base
- ActiveSupport::Testing::Performance::Metrics::CpuTime
- ActiveSupport::Testing::Performance::Metrics::DigitalInformationUnit
- ActiveSupport::Testing::Performance::Metrics::GcRuns
- ActiveSupport::Testing::Performance::Metrics::GcTime
- ActiveSupport::Testing::Performance::Metrics::Memory
- ActiveSupport::Testing::Performance::Metrics::Objects
- ActiveSupport::Testing::Performance::Metrics::ProcessTime
- ActiveSupport::Testing::Performance::Metrics::Time
- ActiveSupport::Testing::Performance::Metrics::UserTime
- ActiveSupport::Testing::Performance::Metrics::WallTime
- ActiveSupport::Testing::Performance::Performer
- ActiveSupport::Testing::Performance::Profiler
- ActiveSupport::Testing::ProxyTestResult
- ActiveSupport::Testing::RemoteError
- ActiveSupport::Testing::SetupAndTeardown
- ActiveSupport::Testing::SetupAndTeardown::ClassMethods
- ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit
- ActiveSupport::Testing::SetupAndTeardown::ForMiniTest
- ActiveSupport::TimeWithZone
- ActiveSupport::TimeZone
- ActiveSupport::XmlMini
- ActiveSupport::XmlMini_LibXMLSAX
- ActiveSupport::XmlMini_LibXMLSAX::HashBuilder
- ActiveSupport::XmlMini_NokogiriSAX
- ActiveSupport::XmlMini_NokogiriSAX::HashBuilder
- Test
- Test::Unit
- Test::Unit::Collector
- Test::Unit::Collector::ObjectSpace
- Object
- Object::HashWithIndifferentAccess
- Object::MissingSourceFile
- ERB
- ERB::Util
- I18n
- I18n::Railtie
- LoadError
- LoadError
- Logger
- Logger::SimpleFormatter
- Array
- Base64
- Benchmark
- BigDecimal
- Class
- Date
- DateTime
- Enumerable
- FalseClass
- File
- Float
- Hash
- Integer
- Kernel
- Module
- NameError
- NilClass
- Numeric
- Process
- QualifiedConstUtils
- Range
- Regexp
- String
- Symbol
- Time
- TrueClass
- URI

No matching classes.

# ActiveSupport::OrderedHash

The order of iteration over hashes in Ruby 1.8 is undefined. For example, you do not know the order in which `keys` will return keys, or `each` yield pairs. `ActiveSupport::OrderedHash` implements a hash that preserves insertion order, as in Ruby 1.9:

    oh = ActiveSupport::OrderedHash.new
    oh[:a] = 1
    oh[:b] = 2
    oh.keys # => [:a, :b], this order is guaranteed

`ActiveSupport::OrderedHash` is namespaced to prevent conflicts with other implementations.

### Public Class Methods

[](*args) click to toggle source

    # File lib/active_support/ordered_hash.rb, line 73
    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

In MRI the Hash class is core and written in C. In particular, methods are programmed with explicit C function calls and polymorphism is not honored.

For example, []= is crucial in this implementation to maintain the @keys array but hash.c invokes rb_hash_aset() originally. This prevents method reuse through inheritance and forces us to reimplement stuff.

For instance, we cannot use the inherited merge! because albeit the algorithm itself would work, our []= is not being called at all by the C code.

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

### Public Instance Methods

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

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

clear() click to toggle source

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

delete(key) click to toggle source

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

delete_if() click to toggle source

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

each() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 160
    def each
      return to_enum(:each) unless block_given?
      @keys.each {|key| yield [key, self[key]]}
      self
    end

each_key() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 148
    def each_key
      return to_enum(:each_key) unless block_given?
      @keys.each { |key| yield key }
      self
    end

each_pair() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 166
    def each_pair
      return to_enum(:each_pair) unless block_given?
      @keys.each {|key| yield key, self[key]}
      self
    end

each_value() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 154
    def each_value
      return to_enum(:each_value) unless block_given?
      @keys.each { |key| yield self[key]}
      self
    end

encode_with(coder) click to toggle source

    # File lib/active_support/ordered_hash.rb, line 28
    def encode_with(coder)
      coder.represent_seq '!omap', map { |k,v| { k => v } }
    end

extractable_options?() click to toggle source

Returns true to make sure that this hash is extractable via `Array#extract_options!`

    # File lib/active_support/ordered_hash.rb, line 51
    def extractable_options?
      true
    end

initialize_copy(other) click to toggle source

    # File lib/active_support/ordered_hash.rb, line 97
    def initialize_copy(other)
      super
      # make a deep copy of keys
      @keys = other.keys
    end

inspect() click to toggle source

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

invert() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 208
    def invert
      OrderedHash[self.to_a.map!{|key_value_pair| key_value_pair.reverse}]
    end

keys() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 132
    def keys
      @keys.dup
    end

merge(other_hash, &block) click to toggle source

    # File lib/active_support/ordered_hash.rb, line 197
    def merge(other_hash, &block)
      dup.merge!(other_hash, &block)
    end

merge!(other_hash) click to toggle source

    # File lib/active_support/ordered_hash.rb, line 186
    def merge!(other_hash)
      if block_given?
        other_hash.each { |k, v| self[k] = key?(k) ? yield(k, self[k], v) : v }
      else
        other_hash.each { |k, v| self[k] = v }
      end
      self
    end

nested_under_indifferent_access() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 46
    def nested_under_indifferent_access
      self
    end

reject(&block) click to toggle source

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

reject!() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 122
    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/active_support/ordered_hash.rb, line 202
    def replace(other)
      super
      @keys = other.keys
      self
    end

shift() click to toggle source

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

sync_keys!() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 217
    def sync_keys!
      @keys.delete_if {|k| !has_key?(k)}
    end

to_a() click to toggle source

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

to_hash() click to toggle source

    # File lib/active_support/ordered_hash.rb, line 140
    def to_hash
      self
    end

to_yaml(opts = {}) click to toggle source

    # File lib/active_support/ordered_hash.rb, line 32
    def to_yaml(opts = {})
      if YAML.const_defined?(:ENGINE) && !YAML::ENGINE.syck?
        return super
      end

      YAML.quick_emit(self, opts) do |out|
        out.seq(taguri) do |seq|
          each do |k, v|
            seq.add(k => v)
          end
        end
      end
    end

to_yaml_type() click to toggle source

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

values() click to toggle source

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.