[RAILS] Getting Started with Ruby Modules

Getting Started with Ruby Modules

It is a module that is a function of Ruby, but I feel that it is one of the parts that is difficult to understand for those who are new to Ruby.

So this time I will summarize the modules.

Module function

● Mixins (include and extend) ● Creating namespaces ● Provide functions and methods

There are other features, but I've listed three main ones. Today I would like to take a look at these.

Mixin

I don't know what it means to be a mixin, Multiple inheritance can be done by incorporating the module into the class. In Ruby, only single inheritance of class is possible, but module can be multiple inheritance. Also, unlike class, the same function can be shared even if it is not an is-a relationship (a relationship that an object is "an instance of a class or its descendant class").

Addendum: The explanation of the is-a relationship has been corrected. Thank you for your comment.

include

ruby.rb


module Hoge
  def hello
    puts 'Hello'
  end
end

module Bar
  def bye
    puts 'Bye'
  end
end

class Greet
  #Include the module created above
  include Hoge
  include Bar
end

greet = Greet.new
greet.hello #=> "Hello"
greet.bye   #=> "Bye"

By including as above, the class can use the methods defined in the module. Adding a function by including a module in a class in this way is called a mixin.

extend You can use extend to turn a method in a module into a class method.

ruby.rb


module Hoge
  def hello
    puts 'Hello'
  end
end

class Greet
  #Extend the module created above
  extend Hoge
end

#You can call hello as a class method
Greet.hello #=> "Hello"

Like this, you can use the methods defined in the module with include or extend in the class. It is a mixin that is one of the ways to use the module.

Providing a namespace

If you write a class in the module name, it means a class that belongs to the module. You can prevent name conflicts.

module Bar
  class Baz
    def self.foo
      puts 'foo'
    end
  end
end

#I called the foo method from the Baz class that belongs to the module called Bar.
Bar::Baz.foo #=> "foo"

What I felt after working as an engineer There are many opportunities to use namespaces. Because the bigger the project, the more risky name conflicts will occur. We often set namespaces in this way to prevent collisions.

Define functions and methods

constant

Constants defined in the module can be called via the module name.

ruby.rb


module Hoge
  Year = "2020"
end

Hoge::Year #=> "2020"

Method

Instance methods can be called by using the module_function method and making the method a module function.

ruby.rb


module Hoge
  def hello
    puts 'Hello'
  end

  module_function :hello
end

Hoge.hello #=> "Hello"

That concludes the module summary. Today was the 10th day of the serialization of an engineer who will become a full-fledged engineer 100 days later.

** 90 days to become a full-fledged engineer **

Recommended Posts

Getting Started with Ruby Modules
Getting Started with Ruby
Getting Started with DBUnit
Getting Started with Swift
Getting Started with Docker
Getting Started with Doma-Transactions
Getting Started with Doma-Annotation Processing
Getting Started with Java Collection
Getting Started with JSP & Servlet
Getting Started with Java Basics
Getting Started with Spring Boot
Getting Started with Java_Chapter 5_Practice Exercises 5_4
[Google Cloud] Getting Started with Docker
Getting started with Java lambda expressions
Getting Started with Docker with VS Code
Getting Started with Doma-Criteria API Cheat Sheet
Ruby Learning # 34 Modules
I started Ruby
Getting Started with Docker for Mac (Installation)
Getting Started with Parameterization Testing in JUnit
Getting Started with Java Starting from 0 Part 1
Getting Started with Ratpack (4)-Routing & Static Content
Getting started with the JVM's GC mechanism
Getting Started with Language Server Protocol with LSP4J
Getting Started with Creating Resource Bundles with ListResoueceBundle
Getting Started with Java_Chapter 8_About Instances and Classes
Links & memos for getting started with Java (for myself)
Getting Started with Doma-Using Projection with the Criteira API
Getting Started with Java 1 Putting together similar things
Getting started with Kotlin to send to Java developers
Getting Started with Doma-Using Joins with the Criteira API
Getting Started with Doma-Introduction to the Criteria API
I tried Getting Started with Gradle on Heroku
Install Ruby 3.0.0 with asdf
Get started with Gradle
Evolve Eevee with Ruby
Getting started with Java programs using Visual Studio Code
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Proceed with Rust official documentation on Docker container (1. Getting started)
Getting started with Java and creating an AsciiDoc editor with JavaFX
Solve Google problems with Ruby
I tried DI with Ruby
Getting Started with Doma-Dynamicly construct WHERE clauses with the Criteria API
GraphQL Client starting with Ruby
Get started with Spring boot
Ruby: Send email with Starttls
Leap year judgment with Ruby
Format Ruby with VS Code
Integer check method with ruby
Get started with DynamoDB with docker
Ruby Learning # 10 Getting User Input
Ruby Learning # 8 Working With String
Completable Future Getting Started (First Future)
[Ruby] problem with if statement
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
[Ruby] REPL-driven development with pry
Getting Started with GitHub Container Registry instead of Docker Hub
[ruby] Method call with argument
Returning to the beginning, getting started with Java ② Control statements, loop statements
Summarize the main points of getting started with JPA learned with Hibernate