加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:50:30 所属栏目:百科 来源:网络整理
导读:在 Ruby类中,我覆盖了三种方法,并且在每种方法中,我基本上都做同样的事情: class ExampleClass def confirmation_required? is_allowed super end def postpone_email_change? is_allowed super end def reconfirmation_required? is_allowed super endend
在 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读