Ruby:扩展自我 [英] Ruby: extend self

查看:28
本文介绍了Ruby:扩展自我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中,我了解extend的基本思想.然而,这段代码中发生了什么?具体来说,extend 做了什么?它只是将实例方法变成类方法的便捷方式吗?为什么要这样做而不是从一开始就指定类方法?

In Ruby, I understand the basic idea of extend. However, what's happening in this segment of code? Specifically, what does extend do? Is it just a convenient way of making the instance methods into class methods? Why would you do it this way rather than specifying class methods from the beginning?

module Rake
  include Test::Unit::Assertions

  def run_tests # etc.
  end

  # what does the next line do?
  extend self
end

推荐答案

将实例方法变成类方法是一种便捷的方式.但您也可以将其用作 更高效单身.

It is a convenient way to make instance methods into class methods. But you can also use it as a more efficient singleton.

这篇关于Ruby:扩展自我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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