A Range represents an interval of values — (1..10) for numbers, ('a'..'z') for letters, etc. Ranges are objects with useful methods, used for iteration, slicing, membership tests, and case matching. They're a concise, expressive Ruby feature.
A Range represents an interval of values — (1..10) for numbers, ('a'..'z') for letters, etc. Ranges are objects with useful methods, used for iteration, slicing, membership tests, and case matching. They're a concise, expressive Ruby feature.
(1..5) # INCLUSIVE — 1, 2, 3, 4, 5 (includes the end)
(1...5) # EXCLUSIVE — 1, 2, 3, 4 (excludes the end)
('a'..'e') # 'a', 'b', 'c', 'd', 'e' — works on letters too
The key distinction: .. includes the end, ... excludes it (the third dot "pushes the end away"). A common point to remember.
(1..5).each { |n| puts n } # iterate — 1 to 5
(1..5).to_a # [1, 2, 3, 4, 5] — convert to an array
(1..10).map { |n| n * 2 } # ranges support Enumerable methods
(1..100).sum # 5050
(1..10).select(&:even?) # [2, 4, 6, 8, 10]
Ranges include Enumerable, so they support each, map, select, etc. — and to_a materializes them into an array.
(1..100).include?(50) # true
(1..100).cover?(50) # true — faster (doesn't iterate, just checks endpoints)
age = 25
(18..65).include?(age) # check if a value falls in a range
include?/cover? test membership efficiently (especially cover?, which just checks endpoints without iterating).
# case/when with ranges — clean range-based branching
grade = case score
when 90..100 then "A"
when 80..89 then "B"
when 0..79 then "F"
end
# array/string slicing with ranges
[10, 20, 30, 40, 50][1..3] # [20, 30, 40] — slice by range
"hello world"[0..4] # "hello" — substring by range
# endless/beginless ranges (Ruby 2.6+)
arr[2..] # from index 2 to the end
(1..) # 1 to infinity (with lazy evaluation)
Ranges are a useful, expressive Ruby feature used in many common scenarios, so understanding them is valuable everyday knowledge.
They concisely represent intervals (numeric, alphabetic) and appear in several frequent contexts: iteration ((1..5).each — a clean way to loop over a sequence), slicing arrays and strings (arr[1..3], str[0..4] — extracting sub-sequences elegantly), membership testing (include?/cover? — checking if a value falls in an interval, with cover? being efficient by just checking endpoints), and especially case/when matching (clean range-based branching, like grading by score ranges — a common, readable pattern).
Understanding the important .. (inclusive) vs ... (exclusive) distinction is necessary to use ranges correctly (and a common point of confusion).
Knowing that ranges include Enumerable (so they support map/select/etc.), how to use them for iteration, slicing, membership, and case matching, and modern features (endless/beginless ranges) covers the practical uses.
Since ranges appear frequently in idiomatic Ruby (iteration, slicing, range-based conditionals, validation of value ranges), and since they're a concise, expressive way to handle intervals, understanding ranges — their syntax (inclusive vs exclusive), methods, and common uses (iteration, slicing, membership, case matching) — is useful, practically-relevant knowledge for writing clean, idiomatic Ruby that handles sequences and intervals elegantly.
A library of IT interview questions with detailed answers — from Junior to Senior.
Donate