[Ruby] Singular methods and singular classes

Singular methods and singular classes

The Ruby concept of methods and classes just for a particular object.

Singular method

--The singular method build_greeting belongs only to the string object referenced by the variable message at the time of definition.

message = "Hello"  #String object
def message.build_greeting(target)
  "#{self}, #{target}."
end
message.build_greeting("world")  # => Hello, world.
message2 = "Hello"  #Objects with the same value but different values
message2.build_greeting("world")  # => NoMethodError

Singleton pattern

--A GoF design pattern that guarantees that only one instance of that class will be created.

CENTRAL_REPOSITORY = Object.new
def CENTRAL_REPOSITORY.register(target)
  @registered_objects ||= []
  unless @resgistered_objects.include? target
    @registered_objects << target
  end
end
def CENTRAL_REPOSITORY.unregister(target)
  @registered_objects = []
  @registered_objects.delete(target)
end

Singularity class

--The definition expression of the singular class is described as "<< (object)" in the class expression. --The object CENTRAL_REPOSITORY now belongs to the singular class(CENTRAL_REPOSITORY). --Instance methods added to the singular class (CENTRAL_REPOSITORY) can be called on the object CENTRAL_REPOSITORY.

CENTRAL_REPOSITORY = Object.new
class << CENTRAL_REPOSITORY
  def register(target)
    @registered_objects ||= []
    unless @registered_objects.include? target
      @registered_objects << target
    end
  end
  def unregister(target)
    @registered_objects ||= []
    @registered_objects.delete(target)
  end
end

Relationship with singular methods

-** A singular method is an instance method of a singular class **. ――In other words, what is happening in the above two examples is the same.

Class methods and metaclasses

-** A class method is a singular method ** of a Class object.

--The pseudovariable self refers to Duration itself, so a_week_from is a peculiar method of Duration.

class Duration
  def self.a_week_from(from)
    return self.new(from, from+7*24&60*60)
  end
end
p Duration.a_week_from(Time.now)

Metaclass

-** A metaclass is a singular class ** of a Class object.

--The singular class (Duration) is also called the metaclass of Duration.

class Duration
  class << self  # self(=Duration)Define a singular class for
    def a_week_from(from)
      return self.new(from, from+7*24*60*60)
    end
  end
end
p Duration.a_week_from(Time.now)

Summary

I was impressed by understanding that "the class of the A class is the Class class (= the A class is the Class object)".

reference

-First Ruby -Ruby Metaclass Hierarchy -Ruby beginner's class << self story (or singular and metaclass) -After all, what is a Ruby peculiar method? -[Ruby] About singular methods

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
Ruby standard input and various methods
How to call classes and methods
Java generics (defines classes and methods)
Classes and instances
About singular methods
Java programming (classes and instances, main methods)
Functions and methods
Memorandum (Ruby: Basic Grammar: Classes and Instances)
JAVA learning history abstract classes and 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)
Symbols and Destructive Ruby
HashMap and HashSet classes
[Ruby] Big Decimal and DECIMAL
Ruby Learning # 29 Classes & Objects
About classes and instances
Ruby Learning # 31 Object Methods
About Ruby instance methods
Ruby inheritance and delegation
[Ruby] methods, instance methods, etc ...
List and happy classes
java (classes and instances)
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
rails path and url methods
Three Bit Manipulation Methods (Ruby)
About Ruby hashes and symbols
[Ruby] Creating code using the concept of classes and instances
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.
[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.
[Ruby] Difference between is_a? And instance_of?