跳至内容 跳至搜索

不区分大小写的哈希

实现了一个哈希,其中键 :foo"foo" 被视为相同。

rgb = ActiveSupport::HashWithIndifferentAccess.new

rgb[:black] = '#000000'
rgb[:black]  # => '#000000'
rgb['black'] # => '#000000'

rgb['white'] = '#FFFFFF'
rgb[:white]  # => '#FFFFFF'
rgb['white'] # => '#FFFFFF'

在内部,当符号用作键时(调用 []=merge 等),它们会被映射到字符串。此映射属于公共接口。例如,给定

hash = ActiveSupport::HashWithIndifferentAccess.new(a: 1)

您可以保证键将作为字符串返回

hash.keys # => ["a"]

从技术上讲,其他类型的键也被接受

hash = ActiveSupport::HashWithIndifferentAccess.new(a: 1)
hash[0] = 0
hash # => {"a"=>1, 0=>0}

但是此类旨在用于字符串或符号是预期键,并且方便将两者视为相同的用例。例如 Ruby on Rails 中的 params 哈希。

请注意,核心扩展定义了 Hash#with_indifferent_access

rgb = { black: '#000000', white: '#FFFFFF' }.with_indifferent_access

这可能很方便。

要在 Rails 外部访问此类,请使用以下命令要求核心扩展

require "active_support/core_ext/hash/indifferent_access"

这将反过来要求此文件。

方法
#
A
C
D
E
F
H
I
K
M
N
R
S
T
U
V
W

类公共方法

[](*args)

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 85
def self.[](*args)
  new.merge!(Hash[*args])
end

new(constructor = nil)

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 70
def initialize(constructor = nil)
  if constructor.nil?
    super()
  elsif constructor.respond_to?(:to_hash)
    super()
    update(constructor)

    hash = constructor.is_a?(Hash) ? constructor : constructor.to_hash
    self.default = hash.default if hash.default
    self.default_proc = hash.default_proc if hash.default_proc
  else
    super(constructor)
  end
end

实例公共方法

[](key)

Hash#[] 相同,其中作为参数传递的键可以是字符串或符号

counters = ActiveSupport::HashWithIndifferentAccess.new
counters[:foo] = 1

counters['foo'] # => 1
counters[:foo]  # => 1
counters[:zoo]  # => nil
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 184
def [](key)
  super(convert_key(key))
end

[]=(key, value)

为哈希分配一个新值

hash = ActiveSupport::HashWithIndifferentAccess.new
hash[:key] = 'value'

此值以后可以使用 :key'key' 来获取。

如果值是 Hash 或包含一个或多个哈希,它们将被转换为 HashWithIndifferentAccess

也别名为:regular_writer
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 101
def []=(key, value)
  regular_writer(convert_key(key), convert_value(value, conversion: :assignment))
end

assoc(key)

Hash#assoc 相同,其中作为参数传递的键可以是字符串或符号

counters = ActiveSupport::HashWithIndifferentAccess.new
counters[:foo] = 1

counters.assoc('foo') # => ["foo", 1]
counters.assoc(:foo)  # => ["foo", 1]
counters.assoc(:zoo)  # => nil
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 197
def assoc(key)
  super(convert_key(key))
end

compact()

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 390
def compact
  dup.tap(&:compact!)
end

deep_symbolize_keys()

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 334
def deep_symbolize_keys; to_hash.deep_symbolize_keys! end

default(key = (no_key = true))

Hash#default 相同,其中作为参数传递的键可以是字符串或符号

hash = ActiveSupport::HashWithIndifferentAccess.new(1)
hash.default                   # => 1

hash = ActiveSupport::HashWithIndifferentAccess.new { |hash, key| key }
hash.default                   # => nil
hash.default('foo')            # => 'foo'
hash.default(:foo)             # => 'foo'
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 239
def default(key = (no_key = true))
  if no_key
    super()
  else
    super(convert_key(key))
  end
end

delete(key)

从哈希中移除指定的键。

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 317
def delete(key)
  super(convert_key(key))
end

dig(*args)

Hash#dig 相同,其中作为参数传递的键可以是字符串或符号

counters = ActiveSupport::HashWithIndifferentAccess.new
counters[:foo] = { bar: 1 }

counters.dig('foo', 'bar')     # => 1
counters.dig(:foo, :bar)       # => 1
counters.dig(:zoo)             # => nil
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 224
def dig(*args)
  args[0] = convert_key(args[0]) if args.size > 0
  super(*args)
end

dup()

返回哈希的浅拷贝。

hash = ActiveSupport::HashWithIndifferentAccess.new({ a: { b: 'b' } })
dup  = hash.dup
dup[:a][:c] = 'c'

hash[:a][:c] # => "c"
dup[:a][:c]  # => "c"
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 280
def dup
  copy_defaults(self.class.new(self))
end

except(*keys)

返回一个不区分大小写的哈希,其中包含除指定键之外的所有内容。

hash = { a: "x", b: "y", c: 10 }.with_indifferent_access
hash.except(:a, "b") # => {c: 10}.with_indifferent_access
hash                 # => { a: "x", b: "y", c: 10 }.with_indifferent_access
也别名为:without
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 325
def except(*keys)
  dup.except!(*keys)
end

extractable_options?()

返回 true,以便 Array#extract_options! 找到此类成员。

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 58
def extractable_options?
  true
end

fetch(key, *extras)

Hash#fetch 相同,其中作为参数传递的键可以是字符串或符号

counters = ActiveSupport::HashWithIndifferentAccess.new
counters[:foo] = 1

counters.fetch('foo')          # => 1
counters.fetch(:bar, 0)        # => 0
counters.fetch(:bar) { |key| 0 } # => 0
counters.fetch(:zoo)           # => KeyError: key not found: "zoo"
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 211
def fetch(key, *extras)
  super(convert_key(key), *extras)
end

fetch_values(*indices, &block)

返回指定索引处值的数组,并在找不到某个键时引发异常。

hash = ActiveSupport::HashWithIndifferentAccess.new
hash[:a] = 'x'
hash[:b] = 'y'
hash.fetch_values('a', 'b') # => ["x", "y"]
hash.fetch_values('a', 'c') { |key| 'z' } # => ["x", "z"]
hash.fetch_values('a', 'c') # => KeyError: key not found: "c"
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 267
def fetch_values(*indices, &block)
  indices.map! { |key| convert_key(key) }
  super
end

has_key?(key)

别名:key?

include?(key)

别名:key?

key?(key)

检查哈希中是否存在与传入参数匹配的键

hash = ActiveSupport::HashWithIndifferentAccess.new
hash['key'] = 'value'
hash.key?(:key)  # => true
hash.key?('key') # => true
也别名为:include?, has_key?, member?
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 167
def key?(key)
  super(convert_key(key))
end

member?(key)

别名:key?

merge(*hashes, &block)

此方法具有与 update 相同的语义,但它不会修改接收者,而是返回一个新的不区分大小写的哈希,其中包含合并结果。

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 287
def merge(*hashes, &block)
  dup.update(*hashes, &block)
end

merge!(*other_hashes, &block)

别名:update

nested_under_indifferent_access()

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 66
def nested_under_indifferent_access
  self
end

regular_update(*other_hashes, &block)

别名:update

regular_writer(key, value)

别名:[]=

reject(*args, &block)

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 342
def reject(*args, &block)
  return to_enum(:reject) unless block_given?
  dup.tap { |hash| hash.reject!(*args, &block) }
end

replace(other_hash)

用 other_hash 替换此哈希的内容。

h = { "a" => 100, "b" => 200 }
h.replace({ "c" => 300, "d" => 400 }) # => {"c"=>300, "d"=>400}
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 312
def replace(other_hash)
  super(cast(other_hash))
end

reverse_merge(other_hash)

类似于 merge,但方向相反:将接收者合并到参数中,并返回一个新的不区分大小写的哈希作为结果

hash = ActiveSupport::HashWithIndifferentAccess.new
hash['a'] = nil
hash.reverse_merge(a: 0, b: 1) # => {"a"=>nil, "b"=>1}
也别名为:with_defaults
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 297
def reverse_merge(other_hash)
  super(cast(other_hash))
end

reverse_merge!(other_hash)

具有与 reverse_merge 相同的语义,但会就地修改接收者。

也别名为:with_defaults!
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 303
def reverse_merge!(other_hash)
  super(cast(other_hash))
end

select(*args, &block)

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 337
def select(*args, &block)
  return to_enum(:select) unless block_given?
  dup.tap { |hash| hash.select!(*args, &block) }
end

slice(*keys)

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 380
def slice(*keys)
  keys.map! { |key| convert_key(key) }
  self.class.new(super)
end

slice!(*keys)

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 385
def slice!(*keys)
  keys.map! { |key| convert_key(key) }
  super
end

store(key, value, convert_value: true)

为哈希分配一个新值

hash = ActiveSupport::HashWithIndifferentAccess.new
hash[:key] = 'value'

此值以后可以使用 :key'key' 来获取。

如果值是 Hash 或包含一个或多个哈希,它们将被转换为 HashWithIndifferentAccess。除非设置了 `convert_value: false`。

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 115
def store(key, value, convert_value: true)
  value = convert_value(value, conversion: :assignment) if convert_value
  regular_writer(convert_key(key), value)
end

symbolize_keys()

也别名为:to_options
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 332
def symbolize_keys; to_hash.symbolize_keys! end

to_hash()

转换为具有字符串键的常规哈希。

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 395
def to_hash
  copy = Hash[self]
  copy.transform_values! { |v| convert_value_to_hash(v) }
  copy_defaults(copy)
end

to_options()

to_options!()

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 335
def to_options!; self end

to_proc()

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 401
def to_proc
  proc { |key| self[key] }
end

transform_keys(hash = NOT_GIVEN, &block)

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 354
def transform_keys(hash = NOT_GIVEN, &block)
  if NOT_GIVEN.equal?(hash)
    if block_given?
      self.class.new(super(&block))
    else
      to_enum(:transform_keys)
    end
  else
    self.class.new(super)
  end
end

transform_keys!(hash = NOT_GIVEN, &block)

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 366
def transform_keys!(hash = NOT_GIVEN, &block)
  if NOT_GIVEN.equal?(hash)
    if block_given?
      replace(copy_defaults(transform_keys(&block)))
    else
      return to_enum(:transform_keys!)
    end
  else
    replace(copy_defaults(transform_keys(hash, &block)))
  end

  self
end

transform_values(&block)

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 347
def transform_values(&block)
  return to_enum(:transform_values) unless block_given?
  dup.tap { |hash| hash.transform_values!(&block) }
end

update(*other_hashes, &block)

就地更新接收者,合并传入的哈希

hash_1 = ActiveSupport::HashWithIndifferentAccess.new
hash_1[:key] = 'value'

hash_2 = ActiveSupport::HashWithIndifferentAccess.new
hash_2[:key] = 'New Value!'

hash_1.update(hash_2) # => {"key"=>"New Value!"}

hash = ActiveSupport::HashWithIndifferentAccess.new
hash.update({ "a" => 1 }, { "b" => 2 }) # => { "a" => 1, "b" => 2 }

参数可以是 ActiveSupport::HashWithIndifferentAccess 或常规 Hash。在这两种情况下,合并都遵循不区分大小写的语义。

如果参数是具有键 :key"key" 的常规哈希,则接收者中只会包含其中一个值,但具体是哪个值未指定。

当提供块时,重复键的值将通过调用块与重复键、接收者中的值以及 other_hash 中的值来确定。重复键的规则遵循不区分大小写的语义

hash_1[:key] = 10
hash_2['key'] = 12
hash_1.update(hash_2) { |key, old, new| old + new } # => {"key"=>22}
也别名为:regular_update, merge!
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 148
def update(*other_hashes, &block)
  if other_hashes.size == 1
    update_with_single_argument(other_hashes.first, block)
  else
    other_hashes.each do |other_hash|
      update_with_single_argument(other_hash, block)
    end
  end
  self
end

values_at(*keys)

返回指定索引处值的数组

hash = ActiveSupport::HashWithIndifferentAccess.new
hash[:a] = 'x'
hash[:b] = 'y'
hash.values_at('a', 'b') # => ["x", "y"]
# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 253
def values_at(*keys)
  keys.map! { |key| convert_key(key) }
  super
end

with_defaults(other_hash)

别名:reverse_merge

with_defaults!(other_hash)

with_indifferent_access()

# File activesupport/lib/active_support/hash_with_indifferent_access.rb, line 62
def with_indifferent_access
  dup
end

without(*keys)

别名:except