str = “just a regular string” def str.title? self.upcase == self end str.title? # => false str.methods.grep(/title?/) # => [:title?] str.singleton_methods #=> [:title?] str.class # => String String.title? #=> NoMethodError
def obj.a_singleton_method; end def MyClass.another_class_method; end
def object.method #方法主体 end
class MyClass
def my_attribute=(value)
@my_attribute =value
end
def my_attribute
@my_attribute
end
end
obj = MyClass.new
obj.my_attribute = ‘x'
obj.my_attribute #=> ‘x'
class MyClass attr_accessor :my_attribue end
class MyClass def my_method; end end obj = MyClass.new def obj.my_singleton_method; end
class << an_object # 自己的代码 end obj = Object.new singleton_class = class << obj self end singleton_class.class # => Class
“abc”.singleton_class # => #<Class: #<String:0xxxxxx>>
class C
def a_method
‘C#a_method()'
end
end
class D < C; end
obj = D.new
class << obj
def a_singleton_method
‘obj#a_singleton_method()'
end
end
obj.singleton_class.superclass #=> D
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有