Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7817

Show
Ignore:
Timestamp:
10/09/07 07:48:59 (11 months ago)
Author:
bitsweat
Message:

Hash is ordered in Ruby 1.9

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/ordered_options.rb

    r4817 r7817  
    11# OrderedHash is namespaced to prevent conflicts with other implementations 
    22module ActiveSupport 
    3   class OrderedHash < Array #:nodoc: 
    4     def []=(key, value) 
    5       if pair = assoc(key) 
    6         pair.pop 
    7         pair << value 
    8       else 
    9         self << [key, value] 
     3  # Hash is ordered in Ruby 1.9! 
     4  if RUBY_VERSION >= '1.9' 
     5    OrderedHash = ::Hash 
     6  else 
     7    class OrderedHash < Array #:nodoc: 
     8      def []=(key, value) 
     9        if pair = assoc(key) 
     10          pair.pop 
     11          pair << value 
     12        else 
     13          self << [key, value] 
     14        end 
    1015      end 
    11     end 
    1216 
    13     def [](key) 
    14       pair = assoc(key) 
    15       pair ? pair.last : nil 
    16     end 
     17      def [](key) 
     18        pair = assoc(key) 
     19        pair ? pair.last : nil 
     20      end 
    1721 
    18     def keys 
    19       collect { |key, value| key } 
    20     end 
     22      def keys 
     23        collect { |key, value| key } 
     24      end 
    2125 
    22     def values 
    23       collect { |key, value| value } 
     26      def values 
     27        collect { |key, value| value } 
     28      end 
    2429    end 
    2530  end