我正在寻找一个名为 duped_index
的问题的解决方案,但我不太了解这个特定的 Hash.new 变量的概念:
def duped_index(arr)
result = Hash.new { |hash, key| hash[key] = [] }
arr.each_with_index do |ele, idx|
result[ele] << idx
end
result.select { |alphabet, indices| indices.length > 1 }
end
p duped_index(["a", "b", "c", "c", "b", "b", "c", "d", "b"]) => # {"b"=>[1,4,5,8], "c"=>[2,3,6]}
您能否向我解释一下 Hash.new block 之间发生了什么? 一般来说,有没有更有效的方法来解决这个问题?
最佳答案
TL;DR
默认值是一种为哈希键声明静态或动态值的方法,而无需提前明确分配给每个键。实际上,这通常用于确保为所有新键返回一些合理的值,而无需对创建的每个键进行显式分配。
您的代码使用哈希初始化的 block 形式来设置默认值。我解释下面的 block 形式,然后用两个更简单的例子进行对比。
使用 block 设置默认值
在 Ruby 中,可以通过多种不同的方式实例化 Hash 对象。一种方法是将 block 传递给 Hash#new .任何没有值的键都会调用这个 block 。
考虑这个相关的例子:
# define a default value using a block
h = Hash.new { |hash, key| hash[key] = [] }
# block dynamically assigns empty array
# to new keys
h.has_key? 'foo' #=> false
h['foo'] #=> []
h.has_key? 'foo' #=> true
这里,h 被分配了一个带有 block 的新 Hash 对象。该 block 基本上分配一个空的 Array 对象作为未给定显式值的 Hash 的新成员的“默认值”。实际上,这意味着在查找先前未分配的键时 block 返回的值将是 []
.
现在考虑:
h = Hash.new { |hash, key| hash[key] = [] }
# block does nothing for assigned keys
h.has_key? 'bar' #=> false
h['bar'] = nil
h['bar'] #=> nil
h.has_key? 'bar' #=> true
注意赋值(甚至是 nil)如何设置期望值。默认值实际上只在第一次访问不存在的键时使用。
为什么要使用 block ?
当您想要在运行时计算默认值,或者当新键的默认值应该是动态的时, block 声明通常更有用。例如:
# define some variable that will change
@foo = 0
# declare a Hash that dynamically calculates
# its default value
h = Hash.new { @foo + 1 }
h['foo'] #=> 1
@foo += 1
h['bar'] #=> 2
但是,除非您需要额外的灵 active ,否则您可以轻松地将 Array 文字传递给构造函数。例如:
# sets default values to `[]` instead of invoking
# a block each time
h = Hash.new []
除非您希望哈希值中的不同键的默认值会发生变化,否则将单个对象分配为默认值而不是 block 通常在语义上更清晰。
另见:哈希#fetch
获得与默认值类似的行为的另一种方法是使用 Hash#fetch 的 block 形式。 .例如,给定一个没有默认值的 Hash,您仍然可以在进行键查找时声明一个默认值:
h = {}
h.fetch 'foo', []
#=> []
#fetch 的语义和用例与 #new 不同,但在像您这样的示例中,实际结果应该是相同的。您采用的方法最终将取决于您试图用代码表达的内容。
关于Ruby Hash.new 带 block 需要深入解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59869743/