I decided to investigate singular classes and singular methods, so I will summarize the contents.
A method that is object-specific
.
It can be defined by writing the object name before the method name to be defined.
Note that the object must already exist when you define it.
class Klass
end
apple = Klass.new
orenge = Klass.new
def apple.tokui_method
puts "I'm apple"
end
apple.tokui_method
# => I'm apple
orenge.tokui_method
# => undefined method `tokui_method'
If the specified object does not exist when defining it, an error will occur.
class Klass
end
def apple.tokui_method
puts "I'm apple"
end
apple = Klass.new
apple.tokui_method
# => undefined local variable or method `apple' for main:Object (NameError)
Calling super
within a singular method will call the method of the same name defined in the class.
There is also the convenience of being able to override that method.
class Klass
def my_name
puts "I'm fruits"
end
end
apple = Klass.new
orenge = Klass.new
def apple.my_name
super
puts "I'm apple"
end
apple.my_name
# => I'm fruits
# => I'm apple
orenge.my_name
# => I'm fruits
When you define a singular method
on an object, the class in which that method is defined is the singular class
.
Unlike regular classes, singular classes are classes used only by certain objects. Therefore, it is forbidden to create objects or subclasses from singular classes.
To check the singular class of an object
Use Object # singleton_class
.
class Klass
end
obj = Klass.new
# => #<Klass:0x00007f8fb584cc08>
TokuiClass = obj.singleton_class
# => #<Class:#<Klass:0x00007f8fb584cc08>>
TokuiClass.new
# => can't create instance of singleton class (TypeError)
class SubTokuiClass < TokuiClass
end
# => can't make subclass of singleton class (TypeError)
Also, the singular methods mentioned above are defined in singular classes.
For singular methods
You can check it with singleton_class.method_defined?
.
class Klass
end
obj = Klass.new
def obj.tokui_method
:tokui_method
end
p obj.class.method_defined? :tokui_method
# => false
p obj.singleton_class.method_defined? :tokui_method
# => true
When an object is created, the singular class for that object does not exist. Singular classes are created in the following cases:
1, Timing to define singular method
2, Timing of evaluating the singular class definition expression
3, Timing to check singular class for object with Object # singleton_class
obj = Klass.new
def obj.tokui_method
:tokui_method
end
class << obj
end
Object # singleton_class
If the singular class does not exist at the timing you tried to check, it will be newly created.
obj.singleton_class
A singular class is a subclass of the object's class.
class Klass
end
obj = Klass.new
obj.singleton_class.supperclass
# => Klass
When a singular class is created for an object, that object becomes an instance of the singular class, but in Ruby
Being an instance of a singular class is now ignored and is always treated as an instance of the object's class.
You can check if it is a direct instance of the argument with Object # instance_of?
, so let's use this.
obj.instance_of? obj.singleton_class
# => false
obj.instance_of? Klass
# => true
The following objects cannot have singular classes in Ruby.
--Numerical value
1.singleton_class
# => can't define singleton (TypeError)
:symble.singleton_class
# => can't define singleton (TypeError)
p true.singleton_class
# => TrueClass
p false.singleton_class
# => FalseClass
p nil.singleton_class
# => NilClass