2
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?

【Ruby】Enumerableモジュールについて理解を深めたい!

Last updated at Posted at 2025-04-07

Enumerable は何をしてくれるモジュール?

Rubyの Enumerable モジュールは、map, select, find, take などの便利な配列風メソッドを提供してくれるモジュールです。

どういう時に使う?

Railsを使っていたら使う機会がないと思うのですが、以下の場合に使用するのかなと思っています。

  1. 自作のクラスを「配列っぽく」使いたいとき
  2. データを逐次処理・フィルタ・変換したい時
  3. クラスの中の配列っぽいデータにメソッドを委譲せずに使いたいとき

スクリプトを実行する時やバッチ処理を実行する時に使えるかな?くらいの感覚だと思っています。

1. 自作のクラスを「配列っぽく」使いたいとき,クラスの中の配列っぽいデータにメソッドを委譲せずに使いたいとき

Enumerableを使用しない場合

class Alphabet
  attr_reader :alphabet

  def initialize
    @alphabet = ("A".."Z").to_a
  end
end

a = Alphabet.new
p a.alphabet.map(&:downcase)           # => ["a", "b", ..., "z"]

Enumerableを使用する場合

class Alphabet
  include Enumerable

  def each
    ("A".."Z").each { |ch| yield ch }
  end
end

a = Alphabet.new
p a.map(&:downcase)           # => ["a", "b", ..., "z"]

Enumerableを使用すると、インスタンスに対してmapメソッドを使用することができるようになります。

2. データを逐次処理・フィルタ・変換したい時

Enumerableを使用しない場合

class Counter
  attr_reader :counter

  def initialize
    @counter = (1..Float::INFINITY).to_a
  end
end

c = Counter.new
p c.counter.lazy.select(&:even?).first(5)  # => [2, 4, 6, 8, 10]

Enumerableを使用する場合

class Counter
  include Enumerable

  def each
    (1..Float::INFINITY).each { |i| yield i }
  end
end

counter = Counter.new
p counter.lazy.select(&:even?).first(5)  # => [2, 4, 6, 8, 10]

Float::INFINITYを使用する場合の処理を書きやすくなったかな?

Enumerableを使用しない場合は無理やり書いてます。(実行したら、止めない限りずっと処理が続きます。)

まとめ

簡単にしか書かなかったですが、Enumerableモジュールを使用することで、ちょっと簡単にループ処理が書けるようになったりするっぽいです。

制約として、eachメソッドをインクルードしたクラスに定義する必要がありますが、eachメソッドをカスタマイズすることで価値が上がるモジュールなのかなと感じました。

2
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
2
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?