0Pricing
Ruby Academy · Lesson

The Enumerable Module

Shared collection powers.

The Enumerable Module 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.

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

Frequently asked questions

Is the “The Enumerable Module” lesson free?

Yes — the full text of “The Enumerable Module” 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 “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; 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 “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