[Ruby] Singular methods and singular classes

Introduction

I decided to investigate singular classes and singular methods, so I will summarize the contents.

Singular method

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

Singularity class

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 the singular class is created

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

Timing to define a singular method
obj = Klass.new

def obj.tokui_method
  :tokui_method
end
Timing of evaluating the singular class definition expression
class << obj
end
Timing to check singular class for object with 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

Objects and singular classes

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

Objects that do not have a singular class

The following objects cannot have singular classes in Ruby.

--Numerical value

Integers and symbles return an error
1.singleton_class
# => can't define singleton (TypeError)
:symble.singleton_class
# => can't define singleton (TypeError)
true, false, nil will return their class as a singular class
p true.singleton_class
# => TrueClass

p false.singleton_class
# => FalseClass

p nil.singleton_class
# => NilClass

Recommended Posts

[Ruby] Singular methods and singular classes
[Ruby] Singular methods and singular classes
Ruby methods and classes (basic)
[Ruby] Classes and instances
Ruby classes and instances
Ruby variables and methods
[Java] Generics classes and generics methods
About Ruby classes and instances
Ruby variables and functions (methods)
Creating Ruby classes and instances
Java abstract methods and classes
Ruby standard input and various methods
How to call classes and methods
Java generics (defines classes and methods)
Classes and instances
Java programming (classes and instances, main methods)
Functions and methods
Memorandum (Ruby: Basic Grammar: Classes and Instances)
JAVA learning history abstract classes and methods
Ruby Learning # 15 Methods
Comparison of JavaScript objects and Ruby classes
Write code using Ruby classes and instances
Ruby and Gem
About Ruby methods
[Ruby] About variable naming rules. Also naming conventions for constants, classes, and methods.
[Java] Personal summary of classes and methods (basic)
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Ruby] Big Decimal and DECIMAL
Ruby Learning # 29 Classes & Objects
About classes and instances
About Ruby instance methods
Ruby inheritance and delegation
[Ruby] methods, instance methods, etc ...
List and happy classes
java (classes and instances)
About the difference between classes and instances in Ruby
Basic methods of Ruby hashes
Coding methods and refactoring principles
Ruby classes are constants, not
Basic methods of Ruby arrays
How to separate words in names in classes, methods, and variables
GraphQL Ruby and actual development
(Note) Java classes / variables / methods
Ruby syntax errors and countermeasures
Three Bit Manipulation Methods (Ruby)
About Ruby hashes and symbols
Ruby C extension and volatile
Summarize Ruby and Dependency Injection
About classes and instances (evolution)
About pluck and ids methods
Consideration about classes and instances
Java methods and method overloads
What are Ruby class methods?
About Ruby and object model
[Ruby] Class methods, instance methods, etc.
The difference between programming with Ruby classes and programming without it
[Ruby] Difference between get and post
About Java static and non-static methods
Write Ruby methods using C (Part 1)
[Ruby] present/blank method and postfix if.
About the equals () and hashcode () methods