【问题标题】:Ruby override class methodRuby 覆盖类方法
【发布时间】:2012-12-07 04:08:11
【问题描述】:

是否有可能,例如,覆盖“puts”方法并将其替换为调用两次“puts”的函数?

例如:

class IO
  def puts str
    puts str
    puts str
  end
end

我知道这个例子很愚蠢,但我需要它来做其他事情。基本上,我想重写一个函数,但我希望重写函数包含对原始重写函数的调用。

【问题讨论】:

标签: ruby class


【解决方案1】:
class IO

  alias puts_orig puts
  def puts str
    puts_orig str
    puts_orig str
  end
end

【讨论】:

    猜你喜欢
    • 2016-02-23
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多