0Pricing
Ruby Academy · Lesson

Creating Ranges

Inclusive and exclusive ranges.

Creating Ranges is a free Ruby Academy lesson on CoddyKit — lesson 1 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 a Range?

A Range represents a sequence between a start and an end value. Write it with two dots: 1..5.

r = 1..5
puts r
puts r.class

Inclusive Ranges

Two dots make an inclusive range that includes the end value. 1..5 covers 1, 2, 3, 4, 5.

puts (1..5).to_a.inspect

Exclusive Ranges

Three dots make an exclusive range that stops before the end value. 1...5 covers 1, 2, 3, 4.

puts (1...5).to_a.inspect

Iterating a Range

Ranges respond to each, so you can loop directly without converting to an array.

(1..3).each { |n| puts "count #{n}" }

Letter Ranges

Ranges work on letters too. 'a'..'e' walks through the alphabet.

puts ('a'..'e').to_a.inspect

Checking Membership

include? and cover? test whether a value falls inside a range. cover? is faster for numbers.

r = 1..10
puts r.include?(5)
puts r.cover?(11)

Ranges in Case Statements

Ranges shine in case statements because === checks membership.

score = 85
grade = case score
        when 90..100 then "A"
        when 80...90 then "B"
        else "C"
        end
puts grade

min, max, and sum

Numeric ranges answer min, max, and sum directly.

r = 1..10
puts r.min
puts r.max
puts r.sum

step Through a Range

step walks a range with a chosen increment.

(0..10).step(2) { |n| print "#{n} " }
puts

Ranges for Slicing

Use a range inside array or string brackets to grab a slice.

letters = %w[a b c d e]
puts letters[1..3].inspect
puts "hello"[0..2]

Endless Ranges

Omit the end for an endless range, handy for slicing to the tail of an array.

nums = [10, 20, 30, 40]
puts nums[1..].inspect

Quick Check

Check your range understanding.

Recap

You learned ranges:

  • 1..5 inclusive vs 1...5 exclusive
  • iterate with each, step
  • letter ranges and case usage
  • min, max, sum, slicing, and endless ranges

Next: the Enumerable module.

puts (1..100).sum

Frequently asked questions

Is the “Creating Ranges” lesson free?

Yes — the full text of “Creating Ranges” 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 “Creating Ranges”?

Inclusive and exclusive ranges. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Creating Ranges” 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