Ruby中这两个方法有什么不同?

时间:2021-06-14 21:28:52

What is difference between these two methods in Ruby?

Ruby中这两个方法有什么不同?

class Mod   

      def doc(str)
          ...
      end

      def Mod::doc(aClass) 
          ...
      end
end

1 个解决方案

#1


6  

Mod::doc()

is a class method, whereas

类方法,然而

doc()

is an instance method. Here's an example of how to use both:

是一个实例方法。这里有一个如何同时使用这两种方法的例子:

class Mod   
    def doc()
        puts 1
    end

    def Mod::doc() 
        puts 2
    end
end

a = Mod.new
a.doc   #=> 1
Mod.doc #=> 2

Here's a question that compares it with

这是一个与之比较的问题。

self.doc()

#1


6  

Mod::doc()

is a class method, whereas

类方法,然而

doc()

is an instance method. Here's an example of how to use both:

是一个实例方法。这里有一个如何同时使用这两种方法的例子:

class Mod   
    def doc()
        puts 1
    end

    def Mod::doc() 
        puts 2
    end
end

a = Mod.new
a.doc   #=> 1
Mod.doc #=> 2

Here's a question that compares it with

这是一个与之比较的问题。

self.doc()