class SomeClass
#method 1
def SomeClass.ClassMethod1
end
def self.ClassMethod2
end
#method 2
class << self
def ClassMethod3
end
end
end
SomeClass.instance_eval do
#method 3
def ClassMethod4
end
#method 4
define_method(:symbol, )
end
#method 5
SomeClass.class_eval do
def self.ClassMethod5
end
#method 6
class << self
def ClassMethod6
end
end
end
#method 7 - dynamically define a class method at run time
def add_class_method(c, m, &b)
eigenclass = class << c; self; end
eigenclass.class_eval {
define_method(m, &b)
}
end
#method 1
def SomeClass.ClassMethod1
end
def self.ClassMethod2
end
#method 2
class << self
def ClassMethod3
end
end
end
SomeClass.instance_eval do
#method 3
def ClassMethod4
end
#method 4
define_method(:symbol, )
end
#method 5
SomeClass.class_eval do
def self.ClassMethod5
end
#method 6
class << self
def ClassMethod6
end
end
end
#method 7 - dynamically define a class method at run time
def add_class_method(c, m, &b)
eigenclass = class << c; self; end
eigenclass.class_eval {
define_method(m, &b)
}
end
Ruby真的是太灵活了,要驾驭Ruby的灵活真不容易。
版权声明:除特别声明外,本站所有文章皆是本站原创,转载请以超链接形式注明出处!