Ruby Academy · Lesson

The Enumerable Module

Shared collection powers.

Lesson 2 of 413 steps

The Enumerable Module is a free Ruby Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

What Is Enumerable?

Enumerable is a module that gives collections like arrays, hashes, and ranges a shared set of powerful methods. Any class with each can mix it in.

puts [1, 2, 3].is_a?(Enumerable)
puts (1..3).is_a?(Enumerable)

include? and member?

include? checks whether a collection contains a value.

nums = [10, 20, 30]
puts nums.include?(20)
puts (1..5).include?(3)

count

count returns the size, or with a block counts matching elements.

nums = [1, 2, 3, 4]
puts nums.count
puts nums.count { |n| n.even? }

min, max, and minmax

Find extremes with min, max, or both at once with minmax.

nums = [4, 1, 7, 3]
puts nums.min
puts nums.max
puts nums.minmax.inspect

sum

sum adds all elements. You can pass a starting value.

puts [1, 2, 3].sum
puts [1, 2, 3].sum(100)

sort and sort_by

sort orders elements; sort_by orders by a computed key.

words = ["pear", "a", "fig"]
puts words.sort.inspect
puts words.sort_by { |w| w.length }.inspect

any? and all?

any? is true if at least one element matches; all? is true only if every element matches.

nums = [2, 4, 6]
puts nums.all? { |n| n.even? }
puts nums.any? { |n| n > 5 }

none? and find

none? is true if nothing matches. find (alias detect) returns the first match.

nums = [1, 3, 5]
puts nums.none? { |n| n.even? }
puts nums.find { |n| n > 2 }

first and take

first grabs the leading element(s); take takes a count from the front and drop skips them.

nums = [10, 20, 30, 40]
puts nums.first(2).inspect
puts nums.drop(2).inspect

group_by

group_by builds a hash that buckets elements by the block result.

nums = [1, 2, 3, 4, 5]
groups = nums.group_by { |n| n.even? ? :even : :odd }
puts groups

each_with_index

each_with_index yields both the element and its position.

%w[a b c].each_with_index do |item, i|
  puts "#{i}: #{item}"
end

Quick Check

Check your Enumerable knowledge.

Recap

Enumerable gives collections superpowers:

  • include?, count, min/max, sum
  • sort/sort_by, any?/all?/none?
  • find, first/take/drop
  • group_by, each_with_index

Next: map, select, and reject in depth.

puts [3, 1, 2].sort.inspect
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “The Enumerable Module” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “The Enumerable Module”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “The Enumerable Module”?

Shared collection powers. 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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “The Enumerable Module” 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. Creating Ranges
  2. The Enumerable Module
  3. map, select, reject
  4. reduce and each_with_object
← Back to Ruby Academy