[Ruby basics] I tried to learn modules (Chapter 1)

1. Module overview

Modules are used for a variety of purposes.

In particular

  1. Add an instance method to your class without inheritance. Or overwrite (mixin)

It is difficult to understand from the above definition alone, so let's learn while actually creating a module.

1. 2. Module definition

** How to make a module **

module module name

Module definition (methods, constants, etc.)

end

(Example)

module.rb


#Define a Greeter module with a hello method
module Greeter
 def hello
   'hello'
 end
end

** At first glance it looks like a class definition, but modules are very different from classes **

References

"Introduction to Ruby for Professionals"

Recommended Posts

[Ruby basics] I tried to learn modules (Chapter 1)
I tried to build Ruby 3.0.0 from source
~ I tried to learn functional programming in Java now ~
I tried DI with Ruby
I tried Rails beginner [Chapter 1]
I tried Rails beginner [Chapter 2]
I tried to verify yum-cron
I tried Jets (ruby serverless)
I tried to summarize the basics of kotlin and java
I tried to summarize the basic grammar of Ruby briefly
I tried to automate LibreOffice Calc with Ruby + PyCall.rb (Ubuntu 18.04)
I tried to chew C # (indexer)
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to explain the method
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried to write code like a type declaration in Ruby
I tried to reimplement Ruby Float (arg, exception: true) with builtin
I tried to make Numeron which is not good in Ruby
I tried to make a parent class of a value object in Ruby
I tried a calendar problem in Ruby
I tried to summarize the methods used
Ruby basics
Ruby basics
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
[Ruby] From the basics to the inject method
[Ruby] I tried to diet the if statement code with the ternary operator
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to summarize the Stream API
I tried to make full use of the CPU core in Ruby
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
[Ruby] I tried to summarize the methods that frequently appear in paiza
I tried to use Selenium like JQuery
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
I tried to touch JavaScript Part.2 Object-oriented
Ruby basics
I tried to implement ModanShogi with Kinx
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
I tried to get the distance from the address string to the nearest station with ruby
I tried to summarize about JVM / garbage collection
I want to sort by tab delimited by ruby
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to implement polymorphic related in Nogizaka.
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to organize the session in Rails
I want to use arrow notation in Ruby
java I tried to break a simple block
I tried to link grafana and postgres [docker-compose]
I tried to develop a man-hour management tool