相关疑难解决方法(0)

使用Ruby的类<< self vs self.method:什么更好?

这个Ruby风格指南告诉我们更好地使用self.method_name而不是class method_name.但为什么?

class TestClass
  # bad
  class << self
    def first_method
      # body omitted
    end

    def second_method_etc
      # body omitted
    end
  end

  # good
  def self.first_method
    # body omitted
  end

  def self.second_method_etc
    # body omitted
  end
end
Run Code Online (Sandbox Code Playgroud)

有性能问题吗?

ruby

59
推荐指数
5
解决办法
3万
查看次数

标签 统计

ruby ×1