0Pricing
Ruby Academy · Lesson

Including Enumerable

Get map/select for free.

Including Enumerable is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Get map/select for Free

Once your class has each, mixing in Enumerable instantly gives you map, select, reduce, sort, min, max, and many more, all built on your each.

class Bag
  include Enumerable
  def initialize(*x); @x = x; end
  def each; @x.each { |i| yield i }; end
end
p Bag.new(1, 2, 3).map { |n| n * 10 }

The Contract

The deal is simple: include Enumerable and define each. Enumerable calls your each internally to power everything else.

class Squares
  include Enumerable
  def initialize(n); @n = n; end
  def each
    (1..@n).each { |i| yield i * i }
  end
end
p Squares.new(5).to_a

Filtering with select

select keeps elements where the block is truthy. reject does the opposite.

class Nums
  include Enumerable
  def initialize(*x); @x = x; end
  def each; @x.each { |i| yield i }; end
end
n = Nums.new(1, 2, 3, 4, 5, 6)
p n.select(&:even?)
p n.reject(&:even?)

Aggregating with reduce

reduce folds the collection into a single value.

class Cart
  include Enumerable
  def initialize(*p); @p = p; end
  def each; @p.each { |i| yield i }; end
end
puts Cart.new(5, 10, 15).reduce(:+)

min, max, sort

Comparison-based methods work as long as your elements are comparable.

class Scores
  include Enumerable
  def initialize(*n); @n = n; end
  def each; @n.each { |i| yield i }; end
end
s = Scores.new(7, 2, 9, 4)
puts s.min
puts s.max
p s.sort

find and include?

find returns the first matching element; include? checks membership.

class Words
  include Enumerable
  def initialize(*w); @w = w; end
  def each; @w.each { |x| yield x }; end
end
w = Words.new("cat", "dog", "bird")
puts w.find { |x| x.length == 4 }
puts w.include?("dog")

group_by and partition

Enumerable offers powerful grouping helpers.

class Nums
  include Enumerable
  def initialize(*x); @x = x; end
  def each; @x.each { |i| yield i }; end
end
n = Nums.new(1, 2, 3, 4, 5)
p n.group_by(&:even?)
p n.partition(&:odd?)

Sorting Needs Comparable Elements

If your elements are custom objects, give them <=> (via Comparable) so sort, min, and max know how to order them.

Item = Struct.new(:price) do
  include Comparable
  def <=>(o); price <=> o.price; end
end
class Shelf
  include Enumerable
  def initialize(*i); @i = i; end
  def each; @i.each { |x| yield x }; end
end
shelf = Shelf.new(Item.new(30), Item.new(10), Item.new(20))
p shelf.min.price

each_with_index

Enumerable also supplies each_with_index and each_with_object for richer iteration.

class Letters
  include Enumerable
  def initialize(*l); @l = l; end
  def each; @l.each { |x| yield x }; end
end
Letters.new("a", "b", "c").each_with_index do |letter, i|
  puts "#{i}: #{letter}"
end

Chaining Methods

Because these methods return arrays or enumerators, you can chain them into expressive pipelines.

class Nums
  include Enumerable
  def initialize(*x); @x = x; end
  def each; @x.each { |i| yield i }; end
end
result = Nums.new(1, 2, 3, 4, 5, 6).select(&:even?).map { |n| n * n }
p result

Putting It Together

Including Enumerable is a huge win:

  • Define each once.
  • Gain map, select, reduce, sort, find, group_by, and dozens more.
  • Add Comparable to elements for ordering methods.
class Range5
  include Enumerable
  def each; (1..5).each { |n| yield n }; end
end
puts Range5.new.sum

Quick Check

Test your understanding of including Enumerable.

Recap

You learned to include Enumerable:

  • Define each, include the module, get the full toolkit.
  • map/select/reduce/find/group_by all run on your each.
  • Element ordering needs <=>.
  • Methods chain into pipelines.
class Tens
  include Enumerable
  def each; [10, 20, 30].each { |n| yield n }; end
end
p Tens.new.map { |n| n + 1 }

Frequently asked questions

Is the “Including Enumerable” lesson free?

Yes — the full text of “Including Enumerable” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Including Enumerable”?

Get map/select for free. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Including Enumerable” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Writing Custom each
  2. Including Enumerable
  3. Lazy Enumerators
  4. Enumerator Objects
← Back to Ruby Academy