0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

モジュールによる共通化

Last updated at Posted at 2020-05-09

#モジュールとは
一連の振る舞いを一箇所にまとめたもの。
クラスと似ているがモジュールはインスタンス (オブジェクト)を生成できない点が異なる。
includeメソッドを使うことでモジュールを使い回せる。

***.rb
#モジュールを作成
module Chatting
  def chat
   "Hello"
  end
end

#モジュールをDogクラスに取り込む
class Dog
  include Chatting
end

#Dogクラスからpochiインスタンスを生成
#pochiをレシーバとしてChattingモジュールのchatメソッドを使用
> pochi = Dog.new
> pochi.chat
=> "Hello"  
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?