为什么 Hash#merge 使用 splat 运算符返回哈希数组数组而不是哈希数组?

Why is Hash#merge using the splat operator returning an Array of Arrays of Hashes instead of an Array of Hashes?

TL;DR

我通过反复试验解决了这个问题,但我对 splat 运算符和 pp 方法如何始终给我一个与我认为拥有的对象不同的对象的理解显然存在差距。我想了解这个差距,并确定一种更好的方法来合并哈希数组。我也希望以后能够更有效地调试这类东西。

首先是代码示例和调试步骤。我的半满意解决方案和更详细的问题在底部。

代码

我正在使用 MRI Ruby 2.6.2。鉴于 class Foo,我希望 Foo#windows 到 return 合并哈希。这是 class:

的最小示例
class Foo
  attr_reader :windows

  def initialize
    @windows = {}
  end

  def pry
    { pry: "stuff pry\r" }
  end 

  def irb
    { irb: "stuff irb\r" }
  end 

  def windows= *hashes
    @windows.merge! *hashes
  end
end

foo = Foo.new
foo.windows = foo.pry, foo.irb

问题(调试)

但是,尝试分配给 foo.windows(或者甚至试图用 foo.windows= foo.pry, foo.irb 帮助解析器减少歧义)我从 REPL 中得到一个例外:

TypeError: no implicit conversion of Array into Hash

但是,如果我使用单例方法修改实例以捕获 *hashes 参数的值,我会看到一个哈希数组,我可以很好地合并它们。考虑以下因素:

def foo.windows= *hashes
  pp *hashes
end
foo.windows = foo.pry, foo.irb
#=> [{:pry=>"stuff pry\r"}, {:irb=>"stuff irb\r"}]

{}.merge *[{:pry=>"stuff pry\r"}, {:irb=>"stuff irb\r"}]
#=> {:pry=>"stuff pry\r", :irb=>"stuff irb\r"}

抓取#pp 的输出让我得到了一些按预期工作的东西。然而,当我更深入地挖掘时,结果发现有些东西在哈希的额外嵌套上分层:

def foo.windows= *hashes
  pp *hashes.inspect
end
foo.windows = foo.pry, foo.irb
"[[{:pry=>\"stuff pry\r\"}, {:irb=>\"stuff irb\r\"}]]"

即使 return 值没有显示,也有一组额外的方括号导致数组被嵌套。我真的不明白他们来自哪里。

什么有效

因此,无论出于何种原因,我都必须展开数组,展平它,然后才能合并:

def foo.windows= *hashes
  @windows.merge! *hashes.flatten
end

# The method still returns unexpected results here, but...
foo.windows = foo.pry, foo.irb
#=> [{:pry=>"stuff pry\r"}, {:irb=>"stuff irb\r"}]

# the value stored in the instance variable is finally correct!
foo.windows
#=> {:pry=>"stuff pry\r", :irb=>"stuff irb\r"}

但是为什么?

是的,我已经设法解决了这个问题。但是,我的问题实际上是关于 为什么 合并散列没有按预期工作,以及额外的嵌套层来自何处。我不期待哈希数组,而是哈希数组。我的理解是否存在差距,或者这是某种奇怪的边缘情况?

更重要的是,为什么调试这么难?我希望 #pp 或 #inspect 向我展示我真正拥有的对象,而不是向我展示一个哈希数组作为 return 值,当我显然有一个包含哈希的数组数组时。

你缺少的是 Ruby parser doesn't allow setter methods with more than one parameter.

当您将多个参数传递给 setter 时,它们会自动放在一个数组中(因为 a = 1, 2a = [1, 2] 的含义相同):

def foo.a=(values)
  pp values
end

foo.a = 1, 2 # [1, 2]

但是,如果您定义一个 splat 参数,由于该数组被视为单个参数,因此会发生这种情况:

def foo.a=(*values)
  pp values
end

foo.a = 1, 2 # [[1, 2]]

首先,*hashes 可以表示两种截然不同的意思:

  • def windows=(*hashes) 表示 "Store all arguments passed to this method into the array hashes in order of appearance."
  • @windows.merge! *hashes 使用 hashes 的项目作为方法调用的各个参数 merge!

但是,当你有赋值方法时,listing several values automatically creates an array:

You can implicitly create an array by listing multiple values when assigning:

a = 1, 2, 3
p a # prints [1, 2, 3]

因此foo.windows(foo.pry, foo.irb)

def windows(*hashes)
    pp hashes
    @windows.merge! *hashes
end

将按预期打印 [{:pry=>"stuff pry\r"}, {:irb=>"stuff irb\r"}]。 但是因为你有一个赋值方法,你应该从你的定义中删除星号。

def windows=(hashes)
  @windows.merge!(*hashes)
end