The Basics of Creating and Using Ruby Modules

ルビー・オン・レール is a wonderful open source full-stack web application framework favoring convention over configuration.  通常アプリケーションの作成に使用される再利用可能で簡単に構成可能なコンポーネントを使用すると、Rails でのアプリケーションの構築がより迅速かつ簡単になり、その結果、生産性が向上し、ビジネスの成長がもたらされます。 柔軟でスケーラブルで、Web 開発者にとってアプリケーションの作成と保守が簡単であるため、開発者の間で注目を集めています。 

ルビー・オン・レール Web アプリケーションを構築する際の作業負荷を軽減するために、既知のエンジニアリング パターンと原則を使用することに重点を置いています。 プログラミングの課題を解決する方法は複数ありますが、Ruby は一般的に使用されるパターンに依存して、Rails Web サイトの保守とアップグレードを容易にすると公言しています。 開発オーバーヘッドを削減するために、特定の問題を解決する小さな Rails コード ブロックを含むオープン ソース ライブラリが一般的に使用されます。 このようなコード ブロックを利用すると、開発者は同じ種類のコードを書くのに時間を費やすことなく、安定した十分にテストされた gem を挿入できます。 Ruby プログラマーのツールボックスの中で最も強力なツールの 1 つはモジュールです。 ルビー Modules provide a structure to collect Ruby classes, methods, and constants into a single, separately named and defined unit.

これは、既存のクラス、メソッド、定数との衝突を回避したり、モジュールの機能をクラスに追加 (混合) したりするのに役立ちます。

Creating a Module in Ruby

Creating a module is similar to creating a class, except you use the module keyword instead of the class keyword.

module MyFirstModule
  def say_hello
    puts "Hello"
  end
end

Modules cannot be instantiated. To use a module, you must either include or extend it within a class.

Using include and extend

  • include

Mixes the module’s methods in as instance methods of the class.

  • extend

Mixes the module’s methods in as class methods of the class.

module ReusableModule
  def module_method
    puts "Module Method: Hi there!"
  end
end

class ClassThatIncludes
  include ReusableModule
end

class ClassThatExtends
  extend ReusableModule
end

puts "Include"
ClassThatIncludes.new.module_method
# => "Module Method: Hi there!"

puts "Extend"
ClassThatExtends.module_method
# => "Module Method: Hi there!"

Other Important Concepts About Modules in Ruby

1. Method Lookup Basics:

module M
  def report
    puts "'report' method in module M"
  end
end

class C
  include M
end

class D < C
end
obj = D.new obj.report

出力:

'report' method in module M

How Method Lookup Works

When a Ruby object receives a message (method call), it searches for the method in this order:

  1. The objects class (D)
  2. Modules included in the class (D)
  3. The superclass (C)
  4. Modules included in the superclass (M)
  5. Continue up the inheritance chain

In this case:

  • D does not define report
  • C does not define report
  • C includes module M
  • M defines report, so Ruby executes it

2. Defining the Same Method More Than Once

If a method is defined multiple times, the last definition takes precedence. This applies to both classes and modules.

module InterestBearing
  def calculate_interest
    puts "Placeholder! We're in module InterestBearing."
  end
end

class BankAccount
  include InterestBearing

  def calculate_interest
    puts "Placeholder! We're in class BankAccount."
    puts "And we're overriding the calculate_interest method!"
  end
end

account = BankAccount.new
account.calculate_interest

出力:

Placeholder! We're in class BankAccount.
And we're overriding the calculate_interest method!

The class method overrides the module method.

3. Mixing in Multiple Ruby Modules with the Same Method Name

module M
  def report
    puts "'report' method in module M"
  end
end

module N
  def report
    puts "'report' method in module N"
  end
end

class C
  include M
  include N
end

c = C.new
c.report

出力:

'report' method in module N

The last included module wins because it appears first in the method lookup path.

4. Including a Ruby Module More Than Once

class C
  include M
  include N
  include M
end

c = C.new
c.report

出力:

'report' method in module N

説明

Re-including a module has no effect if the module is already in the lookup path. Ruby does not move it to the top again. The most recently included new module (N) still takes precedence.

5. Using super with Ruby Modules

The super keyword allows you to call the next method up the method lookup chain.

module M
  def report
    puts "'report' method in module M"
  end
end

class C
  include M

  def report
    puts "'report' method in class C"
    puts "About to trigger the next higher-up report method..."
    super
    puts "Back from the 'super' call."
  end
end

c = C.new
c.report

出力:

'report' method in class C
About to trigger the next higher-up report method...
'report' method in module M
Back from the 'super' call.

What Happens Here

  • Ruby first executes report in class C
  • super calls the next report method in the lookup chain (from module M)
  • Control then returns back to the class method

レールカーマ は、初期段階から RoR に取り組んでおり、RoR の訓練を受けた強力な従業員とともに、Ruby on Rails のエンドツーエンドのコンサルティング、アーキテクチャ、構築、管理、拡張において世界中の企業に非常に信頼される企業に成長しました。 

続きを読む : 

ご連絡ください。

最新のアップデートを購読する

関連記事

投稿者について

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です


jaJapanese