'ArgumentError: wrong number of arguments' on Ruby inspect (same code line, only for some values)

'ArgumentError: wrong number of arguments' on Ruby inspect (same code line, only for some values)

我在努力 时偶然发现了这个 - 但我想这更像是一个 Ruby 语言级别的查询。

我有一个接受四个参数的方法:

def render_node_to_output(node, output, context, skip_output = false)

(对于那些好奇的人,我说的方法是 liquid:4.0.3Liquid::BlockBody::render_node_to_output(在 Linux 上,第 102 行在 gems/liquid-4.0.3/lib/liquid/block_body.rb 上)。

在方法的开头,我 inspect 前三个变量为:

p node
p output
p context
print("\n\n")

这个方法被不同的组件重复调用,在某些时候我得到错误:

/root/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/jekyll-3.8.5/lib/jekyll/filters.rb:292:in `inspect': wrong number of arguments (given 0, expected 1) (ArgumentError)
    from /root/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/liquid-4.0.3/lib/liquid/block_body.rb:105:in `inspect'
    from /root/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/liquid-4.0.3/lib/liquid/block_body.rb:105:in `p'
    from /root/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/liquid-4.0.3/lib/liquid/block_body.rb:105:in `render_node_to_output'

显然 context 变量在某个时候进入某种 "non-existent" (?) 状态。

我尝试在 p context 之前添加 print(context.class)print(context),它们确实显示 相同 ,可接受的值 对于成功和失败的场景Liquid::Context#<Liquid::Context:0x00007fffd43d9948>

我的查询:

(我是 Ruby 的新手,如果我 missed/misused 有任何行话,请见谅。)

_argument 错误说:given 0, expected 1 for inspect。这意味着此处调用的 inspect 需要一个参数。但是 inspect 总是应该在没有参数的情况下被调用。

由此我得出结论,方法 inspect 已为 context.class 重新定义。

更新:

现在,根据 OP 对我的回答的评论,我建议对失败的程序进行以下修改:

首先,我们验证异常确实来自我们怀疑的 inspect 调用;虽然这似乎是显而易见的,但魔鬼从不睡觉,我们想要确定。如果是,我们将仔细查看违规对象的属性:

我们用

替换p context
begin
  # Doing an explicit `inspect` to be in control of what is going on
  ic  = context.inspect
  puts ic
rescue ArgumentError => e
  puts "Exception!"
  puts e
  puts "context is a #{context.class}"
  puts "inspect expects: #{context.class.method(:inspect).parameters}"
end

如果真的像 OP 声称的那样,此代码显示 Exception!,但仍显示空参数数组,我会将这个确切的示例报告给Ruby 错误跟踪器。