1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

commit review: say clearly that AS::OrderedHash is about insertion order, be more neutral in wording, do not imply lack of ordering is a problem

This commit is contained in:
Xavier Noria 2010-08-06 13:11:44 +02:00
parent cdbc880055
commit e0a0638094
2 changed files with 14 additions and 12 deletions

View file

@ -4,15 +4,17 @@ YAML.add_builtin_type("omap") do |type, val|
ActiveSupport::OrderedHash[val.map(&:to_a).map(&:first)]
end
# Hash is not ordered in ruby 1.8.x. What it means is there is no guarantee of order of keys when
# method Hash#keys in invoked. Similarly Hash#values and Hash#each can't guarantee that each time
# the output will contain exactly same value in the same order. <tt>OrderedHash</tt> solves that
# problem.
#
# ActiveSupport::OrderedHash[:boy, 'John', :girl, 'Mary']
#
# OrderedHash is namespaced to prevent conflicts with other implementations.
module ActiveSupport
# 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. <tt>ActiveSupport::OrderedHash</tt>
# 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
#
# <tt>ActiveSupport::OrderedHash</tt> is namespaced to prevent conflicts with other implementations.
class OrderedHash < ::Hash #:nodoc:
def to_yaml_type
"!tag:yaml.org,2002:omap"

View file

@ -5,16 +5,16 @@ require 'active_support/ordered_hash'
# h = ActiveSupport::OrderedOptions.new
# h[:boy] = 'John'
# h[:girl] = 'Mary'
# h[:boy] #=> 'John'
# h[:girl] #=> 'Mary'
# h[:boy] # => 'John'
# h[:girl] # => 'Mary'
#
# Using <tt>OrderedOptions</tt> above code could be reduced to:
#
# h = ActiveSupport::OrderedOptions.new
# h.boy = 'John'
# h.girl = 'Mary'
# h.boy #=> 'John'
# h.girl #=> 'Mary'
# h.boy # => 'John'
# h.girl # => 'Mary'
#
module ActiveSupport #:nodoc:
class OrderedOptions < OrderedHash