包含模块时__callee__的意外值 - 这是一个Ruby错误吗? [英] Unexpected value of __callee__ when including a module – is this a Ruby bug?

查看:202
本文介绍了包含模块时__callee__的意外值 - 这是一个Ruby错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当通过 alias_method 创建的方法调用时, __ callee __ 忽略旧方法的名称(此处 xxx )并返回新方法的名称,如下所示:

When invoked via a method created by alias_method, __callee__ ignores the name of the old method (here xxx) and returns the name of the new method, as below:

class Foo
  def xxx() __callee__ end
  alias_method :foo, :xxx
end

Foo.new.foo # => :foo

即使 xxx ,此行为仍然存在继承自超类:

This behavior holds even when xxx is inherited from a superclass:

class Sup
  def xxx() __callee__ end
end

class Bar < Sup
  alias_method :bar, :xxx
end

Bar.new.bar # => :bar

鉴于上述两种情况,我预计当<$ c时会出现相同的行为$ c> xxx 包含在模块中。然而,情况并非如此:

Given both of the above, I would expect that the same behavior would hold when xxx is included via a module. However, that is not the case:

module Mod
  def xxx() __callee__ end
end

class Baz
  include Mod
  alias_method :baz, :xxx
end

Baz.new.baz # => :xxx

我希望返回值为:baz ,而不是:xxx

I expect the return value to be :baz, not :xxx.

上述代码是使用Ruby 2.3.1p112执行的。这是 __ callee __ 的实现中的错误吗?或者可能是 alias_method ?如果没有,有人可以解释为什么模块包含行为不同吗?

The above code was executed using Ruby 2.3.1p112. Is this a bug in the implementation of __callee__? Or maybe of alias_method? And if not, can anyone explain why module inclusion behaves differently?

更新1

我已将此内容发布到Ruby bug跟踪器试图挑起答案。

更新2

显然,我不是唯一一个对这个问题感到惊讶。我想知道是否修订版50728 (这是为了解决错误11046: __ callee __ 返回错误的方法orphan proc中的名称 )可能是相关的。

Apparently, I'm not the only one to be surprised by this issue. I wonder whether Revision 50728 (which was meant to solve Bug 11046: __callee__ returns incorrect method name in orphan proc) might be related.

推荐答案

这是一个错误,它是3天前关闭此笔记

This was a bug, and it was closed 3 days ago with this note:


似乎由 r56592

这篇关于包含模块时__callee__的意外值 - 这是一个Ruby错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆