问:
Hi
I have code like
class A
class <<self
def first
-------
end
def second
------
end
end
end
What I understood is now the def first and second becomes class methods of class A..Am I right? Is that its only use?
Thanks in advance
Sijo
答:
Yes you understood it well, and it's its only use. You could also type:
class A
def self.first
end
end
or
class << A
def first
end
end
Hi
I have code like
class A
class <<self
def first
-------
end
def second
------
end
end
end
What I understood is now the def first and second becomes class methods of class A..Am I right? Is that its only use?
Thanks in advance
Sijo
答:
Yes you understood it well, and it's its only use. You could also type:
class A
def self.first
end
end
or
class << A
def first
end
end
本文讨论了如何在Ruby中定义类方法。通过不同的语法形式,如使用`def self.方法名`或`class << self`来定义类方法,这些方法成为了类本身的一部分而非其实例的方法。文章澄清了这一概念,并解释了其用途。
4364

被折叠的 条评论
为什么被折叠?



