如何在Ruby中重新定义Fixnum的(加)方法并保留原始功能?

前端之家收集整理的这篇文章主要介绍了如何在Ruby中重新定义Fixnum的(加)方法并保留原始功能?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这引发了我在1.9.2 Ruby中的SystemStackError(但在Rubinius中工作):
  1. class Fixnum
  2. def +(other)
  3. self + other * 2
  4. end
  5. end

但是没有超级(基于其他错误).

如何访问原始功能

解决方法

使用alias_method.别名Fixnum是其他东西,然后在新的中引用它:
  1. class Fixnum
  2. alias_method :old_add,:+
  3. def +(other)
  4. self.old_add(other) * 2
  5. end
  6. end

猜你在找的Ruby相关文章