微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby – 覆盖类似的方法,缩短语法

Ruby类中,我覆盖了三种方法,并且在每种方法中,我基本上都做同样的事情:

class ExampleClass

  def confirmation_required?
    is_allowed && super
  end

  def postpone_email_change?
    is_allowed && super
  end

  def reconfirmation_required?
    is_allowed && super
  end

end

是否有更紧凑的语法?如何缩短代码

解决方法

如何使用别名?

class ExampleClass

  def confirmation_required?
    is_allowed && super
  end

  alias postpone_email_change? confirmation_required?
  alias reconfirmation_required? confirmation_required?
end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐