What is a Ruby module?

What is a module?

A module is a collection of processes.

#Example: Caluculate module that summarizes addition and subtraction processing
module Calculate
  def add(a, b)
    a + b
  end

  def sub(a, b)
    a - b
  end
end

Difference between module and class

Modules and classes are similar, but with the following differences:

Module usage

The module is used for the following purposes.

Providing namespaces

The same class can be used as a different class by putting it in a module with a different name.

#Example: Include the same Image class in another module and different classes (User)::Image and Post::Treat as Image)
module User
  class Image
    def self.hoge
      puts 'user image'
    end
  end
end

module Post
  class Image
    def self.hoge
      puts 'post image'
    end
  end
end

User::Image.hoge #=> "user image"
Post::Image.hoge #=> "post image"

Incorporate into a class as an instance method

You can use the include method to include the module processing as an instance method of the target class.

#Example: Incorporate User module as an instance method of Image class

module User
  def hoge
    puts 'user image'
  end
end

class Image
  include User
end

image = Image.new
image.hoge #=> "user image"

Incorporate into a class as a class method

You can use the extend method to import the module processing as a class method of the target class.

#Example: Incorporate User module as class method of Image class

module User
  def hoge
    puts 'user image'
  end
end

class Image
  extend User
end

Image.hoge #=> "user image"

Summary

Recommended Posts

What is a Ruby module?
What is a Ruby 2D array?
What is a constructor?
What is a stream
What is a Servlet?
[Ruby] What is true?
What is a wrapper class?
What is a boolean type?
What is a floating point?
What is a meaningful comment?
What is a jar file?
What is a Java collection?
What is a lambda expression?
[Ruby] What is `!!` used for?
[Ruby] What is an instance?
What is a fa⁉ enum?
What is a safety reference operator (&.) Using Ruby ampersand?
[Ruby] Module
[Ruby on Rails] What is Bcrypt?
What is a snippet in programming?
What is a column Boolean type?
What is a reference type variable?
What is a lambda expression (Java)
[Swift] What is "inheriting a class"?
What is Cubby
What is null? ]
What is a Spring Boot .original file?
What is java
What is Keycloak
What is maven?
[Technical memo] What is "include" in Ruby?
What is Jackson?
[For programming beginners] What is a method?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is a class in Java language (1 /?)
What is params
What is SLF4J?
What is a class in Java language (2 /?)
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
[Rails] What is a dot (.) Or a colon (:)?
What is programming?
What is before_action?
What is Docker
What is a request scope? Image commentary
What is Byte?
What is Tomcat
Introduction to Recursive Functions: What is a Recursive Function?
Ruby sum is a sober and amazing story
About Ruby Kernel Module
What is Maven Assembly?
What is `docker-compose up`?