Klasa szyn << self


Chciałbym się dowiedzieć, do czego służy
class & < & < self
w poniższym przykładzie.
module Utility
class Options #:nodoc:
class << self
def parse(args)
end
end
end
end

Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:


to
>
module Utility
class Options #:nodoc:
class << self
# we are inside Options's singleton class
def parse(args) end
end
end
end


równoważny:
>
module Utility
class Options #:nodoc:
def Options.parse(args) end
end
end


Kilka przykładów, które pomogą Ci zrozumieć:
>
class A
HELLO = 'world'
def self.foo
puts "class method A::foo, HELLO #{HELLO}"
end def A.bar
puts "class method A::bar, HELLO #{HELLO}"
end class << self
HELLO = 'universe'
def zim
puts "class method A::zim, HELLO #{HELLO}"
end
endend
A.foo
A.bar
A.zim
puts "A::HELLO #{A::HELLO}"# Output
# class method A::foo, HELLO world
# class method A::bar, HELLO world
# class method A::zim, HELLO universe
# A::HELLO world
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

To klasa sama w sobie. To pytanie do mnie

już zapytałem
https://coderoad.ru/1630815/
.

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się