- A
- C
- D
- E
- F
- N
- R
- S
- T
- W
类公共方法
from_trusted_xml(xml) 链接
从 XML 构建一个 Hash,就像 Hash.from_xml 一样,但它还允许 Symbol 和 YAML。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/conversions.rb, line 133 def from_trusted_xml(xml) from_xml xml, [] end
from_xml(xml, disallowed_types = nil) 链接
返回一个包含键值对集合的 Hash,其中键是节点名称,值是其内容
xml = <<-XML <?xml version="1.0" encoding="UTF-8"?> <hash> <foo type="integer">1</foo> <bar type="integer">2</bar> </hash> XML hash = Hash.from_xml(xml) # => {"hash"=>{"foo"=>1, "bar"=>2}}
如果 XML 包含类型为 type="yaml" 或 type="symbol" 的属性,则会引发 DisallowedType 错误。使用 Hash.from_trusted_xml 来解析此 XML。
自定义 disallowed_types 也可以以数组形式传递。
xml = <<-XML <?xml version="1.0" encoding="UTF-8"?> <hash> <foo type="integer">1</foo> <bar type="string">"David"</bar> </hash> XML hash = Hash.from_xml(xml, ['integer']) # => ActiveSupport::XMLConverter::DisallowedType: Disallowed type attribute: "integer"
请注意,传递自定义的 disallowed_types 会覆盖默认类型(即 Symbol 和 YAML)。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/conversions.rb, line 128 def from_xml(xml, disallowed_types = nil) ActiveSupport::XMLConverter.new(xml, disallowed_types).to_h end
实例公共方法
assert_valid_keys(*valid_keys) 链接
验证哈希中的所有键是否与 *valid_keys 匹配,如果不匹配则引发 ArgumentError。
请注意,键的处理方式与 HashWithIndifferentAccess 不同,这意味着字符串键和符号键不会匹配。
{ name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
{ name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
{ name: 'Rob', age: '28' }.assert_valid_keys(:name, :age) # => passes, raises nothing
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 48 def assert_valid_keys(*valid_keys) valid_keys.flatten! each_key do |k| unless valid_keys.include?(k) raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}") end end end
compact_blank!() 链接
原地移除哈希中的所有空白值并返回自身。使用 Object#blank? 来判断值是否为空。
h = { a: "", b: 1, c: nil, d: [], e: false, f: true } h.compact_blank! # => { b: 1, f: true }
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 244 def compact_blank! # use delete_if rather than reject! because it always returns self even if nothing changed delete_if { |_k, v| v.blank? } end
deep_dup() 链接
返回哈希的深层副本。
hash = { a: { b: 'b' } } dup = hash.deep_dup dup[:a][:c] = 'c' hash[:a][:c] # => nil dup[:a][:c] # => "c"
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/object/deep_dup.rb, line 43 def deep_dup hash = dup each_pair do |key, value| if ::String === key || ::Symbol === key hash[key] = value.deep_dup else hash.delete(key) hash[key.deep_dup] = value.deep_dup end end hash end
deep_merge(other_hash, &block) 链接
返回一个新哈希,其中包含递归合并的 self 和 other_hash。
h1 = { a: true, b: { c: [1, 2, 3] } } h2 = { a: false, b: { x: [3, 4, 5] } } h1.deep_merge(h2) # => { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
与标准库中的 Hash#merge 一样,可以提供一个块来合并值
h1 = { a: 100, b: 200, c: { c1: 100 } } h2 = { b: 250, c: { c1: 200 } } h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val } # => { a: 100, b: 450, c: { c1: 300 } }
来源: 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/deep_merge.rb, line 9
deep_merge!(other_hash, &block) 链接
与 deep_merge 相同,但会修改 self。
来源: 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/deep_merge.rb, line 31
deep_stringify_keys() 链接
返回一个新哈希,其中所有键都转换为字符串。这包括根哈希以及所有嵌套哈希和数组的键。
hash = { person: { name: 'Rob', age: '28' } } hash.deep_stringify_keys # => {"person"=>{"name"=>"Rob", "age"=>"28"}}
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 84 def deep_stringify_keys deep_transform_keys { |k| Symbol === k ? k.name : k.to_s } end
deep_stringify_keys!() 链接
原地将所有键转换为字符串。这包括根哈希以及所有嵌套哈希和数组的键。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 91 def deep_stringify_keys! deep_transform_keys! { |k| Symbol === k ? k.name : k.to_s } end
deep_symbolize_keys() 链接
返回一个新哈希,其中所有键都转换为符号(只要它们响应 to_sym)。这包括根哈希以及所有嵌套哈希和数组的键。
hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } } hash.deep_symbolize_keys # => {:person=>{:name=>"Rob", :age=>"28"}}
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 103 def deep_symbolize_keys deep_transform_keys { |key| key.to_sym rescue key } end
deep_symbolize_keys!() 链接
原地将所有键转换为符号(只要它们响应 to_sym)。这包括根哈希以及所有嵌套哈希和数组的键。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 110 def deep_symbolize_keys! deep_transform_keys! { |key| key.to_sym rescue key } end
deep_transform_keys(&block) 链接
返回一个新哈希,其中所有键都通过块操作进行转换。这包括根哈希以及所有嵌套哈希和数组的键。
hash = { person: { name: 'Rob', age: '28' } } hash.deep_transform_keys{ |key| key.to_s.upcase } # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}}
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 65 def deep_transform_keys(&block) _deep_transform_keys_in_object(self, &block) end
deep_transform_keys!(&block) 链接
通过块操作原地转换所有键。这包括根哈希以及所有嵌套哈希和数组的键。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 72 def deep_transform_keys!(&block) _deep_transform_keys_in_object!(self, &block) end
deep_transform_values(&block) 链接
返回一个新哈希,其中所有值都通过块操作进行转换。这包括根哈希以及所有嵌套哈希和数组的值。
hash = { person: { name: 'Rob', age: '28' } } hash.deep_transform_values{ |value| value.to_s.upcase } # => {person: {name: "ROB", age: "28"}}
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/deep_transform_values.rb, line 12 def deep_transform_values(&block) _deep_transform_values_in_object(self, &block) end
deep_transform_values!(&block) 链接
通过块操作原地转换所有值。这包括根哈希以及所有嵌套哈希和数组的值。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/deep_transform_values.rb, line 19 def deep_transform_values!(&block) _deep_transform_values_in_object!(self, &block) end
except!(*keys) 链接
移除给定键从哈希中并返回它。
hash = { a: true, b: false, c: nil } hash.except!(:c) # => { a: true, b: false } hash # => { a: true, b: false }
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/except.rb, line 8 def except!(*keys) keys.each { |key| delete(key) } self end
extract!(*keys) 链接
移除并返回匹配给定键的键值对。
hash = { a: 1, b: 2, c: 3, d: 4 } hash.extract!(:a, :b) # => {:a=>1, :b=>2} hash # => {:c=>3, :d=>4}
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/slice.rb, line 24 def extract!(*keys) keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) } end
extractable_options?() 链接
默认情况下,只有 Hash 本身是可提取的。 Hash 的子类可以实现此方法并返回 true 来声明它们是可提取的。如果一个 Hash 是可提取的, Array#extract_options! 会在它位于 Array 的最后一个元素时将其弹出。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/array/extract_options.rb, line 9 def extractable_options? instance_of?(Hash) end
nested_under_indifferent_access() 链接
当对象嵌套在接收 with_indifferent_access 的对象下时调用。此方法将由封闭对象调用,默认情况下它会别名为 with_indifferent_access。 Hash 的子类可以覆盖此方法以返回 self,如果转换为 ActiveSupport::HashWithIndifferentAccess 是不理想的。
b = { b: 1 } { a: b }.with_indifferent_access['a'] # calls b.nested_under_indifferent_access # => {"b"=>1}
reverse_merge(other_hash) 链接
将调用者合并到 other_hash 中。例如,
options = options.reverse_merge(size: 25, velocity: 10)
等同于
options = { size: 25, velocity: 10 }.merge(options)
这对于使用默认值初始化选项哈希特别有用。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/reverse_merge.rb, line 14 def reverse_merge(other_hash) other_hash.merge(self) end
reverse_merge!(other_hash) 链接
破坏性的 reverse_merge。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/reverse_merge.rb, line 20 def reverse_merge!(other_hash) replace(reverse_merge(other_hash)) end
slice!(*keys) 链接
用给定的键替换哈希。返回一个包含已移除键值对的哈希。
hash = { a: 1, b: 2, c: 3, d: 4 } hash.slice!(:a, :b) # => {:c=>3, :d=>4} hash # => {:a=>1, :b=>2}
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/slice.rb, line 10 def slice!(*keys) omit = slice(*self.keys - keys) hash = slice(*keys) hash.default = default hash.default_proc = default_proc if default_proc replace(hash) omit end
stringify_keys() 链接
返回一个新哈希,其中所有键都转换为字符串。
hash = { name: 'Rob', age: '28' } hash.stringify_keys # => {"name"=>"Rob", "age"=>"28"}
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 10 def stringify_keys transform_keys { |k| Symbol === k ? k.name : k.to_s } end
stringify_keys!() 链接
原地将所有键转换为字符串。与 stringify_keys 相同,但会修改 self。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 16 def stringify_keys! transform_keys! { |k| Symbol === k ? k.name : k.to_s } end
symbolize_keys() 链接
返回一个新哈希,其中所有键都转换为符号(只要它们响应 to_sym)。
hash = { 'name' => 'Rob', 'age' => '28' } hash.symbolize_keys # => {:name=>"Rob", :age=>"28"}
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 27 def symbolize_keys transform_keys { |key| key.to_sym rescue key } end
symbolize_keys!() 链接
原地将所有键转换为符号(只要它们响应 to_sym)。与 symbolize_keys 相同,但会修改 self。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/keys.rb, line 34 def symbolize_keys! transform_keys! { |key| key.to_sym rescue key } end
to_query(namespace = nil) 链接
返回适合用作 URL 查询字符串的接收者的字符串表示形式
{name: 'David', nationality: 'Danish'}.to_query
# => "name=David&nationality=Danish"
可以传递一个可选的命名空间来包含键名
{name: 'David', nationality: 'Danish'}.to_query('user')
# => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
构成查询字符串的字符串对“key=value”按升序词典顺序排序。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/object/to_query.rb, line 81 def to_query(namespace = nil) query = filter_map do |key, value| unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty? value.to_query(namespace ? "#{namespace}[#{key}]" : key) end end query.sort! unless namespace.to_s.include?("[]") query.join("&") end
to_xml(options = {}) 链接
返回一个包含接收者 XML 表示形式的字符串
{ foo: 1, bar: 2 }.to_xml
# =>
# <?xml version="1.0" encoding="UTF-8"?>
# <hash>
# <foo type="integer">1</foo>
# <bar type="integer">2</bar>
# </hash>
为此,该方法遍历键值对并构建依赖于*值*的节点。给定一个键值对 key, value
-
如果
value是一个哈希,则使用key作为:root进行递归调用。 -
如果
value是一个数组,则使用key作为:root进行递归调用,并将key的单数形式作为:children。 -
如果
value是一个可调用对象,它必须接受一个或两个参数。根据参数数量,可调用对象将使用options哈希作为第一个参数,key作为:root,key的单数形式作为第二个参数。可调用对象可以通过使用options[:builder]来添加节点。{foo: lambda { |options, key| options[:builder].b(key) }}.to_xml # => "<b>foo</b>" -
如果
value响应to_xml,则使用key作为:root调用该方法。class Foo def to_xml(options) options[:builder].bar 'fooing!' end end { foo: Foo.new }.to_xml(skip_instruct: true) # => # <hash> # <bar>fooing!</bar> # </hash>
-
否则,将创建一个以
key作为标签的节点,并将其值的字符串表示作为文本节点。如果value为nil,则添加一个名为“nil”且值为“true”的属性。除非:skip_types选项存在且为 true,否则还会根据以下映射添加一个名为“type”的属性XML_TYPE_NAMES = { "Symbol" => "symbol", "Integer" => "integer", "BigDecimal" => "decimal", "Float" => "float", "TrueClass" => "boolean", "FalseClass" => "boolean", "Date" => "date", "DateTime" => "dateTime", "Time" => "dateTime" }
默认根节点是“hash”,但这可以通过 :root 选项进行配置。
默认 XML 构建器是 Builder::XmlMarkup 的新实例。您可以使用 :builder 选项配置自己的构建器。该方法还接受 :dasherize 等选项,它们会转发给构建器。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/conversions.rb, line 74 def to_xml(options = {}) require "active_support/builder" unless defined?(Builder::XmlMarkup) options = options.dup options[:indent] ||= 2 options[:root] ||= "hash" options[:builder] ||= Builder::XmlMarkup.new(indent: options[:indent]) builder = options[:builder] builder.instruct! unless options.delete(:skip_instruct) root = ActiveSupport::XmlMini.rename_key(options[:root].to_s, options) builder.tag!(root) do each { |key, value| ActiveSupport::XmlMini.to_tag(key, value, options) } yield builder if block_given? end end
with_indifferent_access() 链接
返回一个接收者的 ActiveSupport::HashWithIndifferentAccess。
{ a: 1 }.with_indifferent_access['a'] # => 1
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/hash/indifferent_access.rb, line 9 def with_indifferent_access ActiveSupport::HashWithIndifferentAccess.new(self) end