Ruby ranges are cool
We've probably all used or seen ranges in ruby, like these simple examples from the ruby docs:
(1..5).to_a #=> [1, 2, 3, 4, 5] ('a'..'e').to_a #=> ["a", "b", "c", "d", "e"]
And the three dots inclusive range variant:
('a'...'e').to_a #=> ["a", "b", "c", "d"]
But I recently discovered some other cool things, like strings that behave like numbers when I wanted a simple array of 2-digit month numbers:
('01'..'12').to_a #=> ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
And this from Ruby Inside, using ranges instead of complex comparisons for numbers. No more if x >= 1970 && x <= 1979 nonsense, instead:
case year when 1970..1979 "Seventies" when 1980..1989 "Eighties" when 1990..1999 "Nineties" end
Cool!
- Jon Waghorn









