Rails类<<自 [英] Rails class << self
本文介绍了Rails类<<自的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想了解 class<<
I would like to understand what class << self
stands for in the next example.
module Utility
class Options #:nodoc:
class << self
def parse(args)
end
end
end
end
推荐答案
此
this
module Utility
class Options #:nodoc:
class << self
# we are inside Options's singleton class
def parse(args)
end
end
end
end
等效于:
is equivalent to:
module Utility
class Options #:nodoc:
def Options.parse(args)
end
end
end
几个例子可以帮助您理解:
A couple examples to help you understand :
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
end
end
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
这篇关于Rails类<<自的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文