Ranges: from … to
In lesson 6 you counted in a loop with (1..5).each. That 1..5 is an object of its own, a range: everything from a start to an end. With two dots the end belongs to it, with three it doesn't – handy for opening hours: a bakery that opens at 7 and closes at 12 is open in the hours 7...12, and at 12 the door is already locked. to_a turns a range into an array, so you can see what is inside:
dice = 1..6
bakery = 7...12 # at 12 the door is locked
p dice.to_a
p bakery.to_a
bakery.sizeA range only remembers its start and its end, not everything in between – 1..1_000_000 is no bigger than 1..6. Still it answers many of the questions you know from arrays: size, max, include?, sum. And for sum Ruby doesn't even add up a million numbers: it uses the formula young Carl Friedrich Gauss is said to have found at school – (first + last) · count / 2:
million = 1..1_000_000
p million.size
p million.max
p million.include?(4711)
million.sumRanges work with anything that knows what comes next – letters too: "a".."e" are the letters a to e. When you ask whether something is in a range of letters, there is a fine difference: include? asks “is it one of the elements?”, cover? only “does it lie between the start and the end?”. And in the alphabet “chunky” lies between a and z – just as in a dictionary:
letters = "a".."z"
p letters.first(5)
p letters.include?("chunky") # one of the 26 letters? no
letters.cover?("chunky") # between "a" and "z"? yesWith numbers the two mean the same, and they work with decimals too, which no array could ever list. Remember case/when from lesson 5, the decisions? when 36..38 asks the range exactly this question. Kaz tests her bath water:
water = 37.5
p (36..38).include?(water)
case water
when 0...36 then "too cold"
when 36..38 then "just right"
else "too hot!"
endTry water = 38.5: that lies above 36..38, and Kaz jumps out again. Next, step: it walks through a range in bigger strides – the range version of 0.step(10, 5). With a block it runs the block at every stride, without one to_a collects them. Decimals work too:
(0..20).step(5) { |minute| puts "minute #{minute}: stir the porridge" }
p (1..10).step(3).to_a
(0.0..1.0).step(0.25).to_aA range doesn't even need an end. (1..) counts from 1 on and on – an endless range. That is handy when you don't know how far you will have to go. Just never ask it for everything: to_a refuses with a RangeError, and an each without break would count until the end of time. first(5) takes only what you need, and break stops the loop – here as soon as Chunky's bacon stash, which doubles every day, is over 500 strips:
p (1..).first(5)
(1..).each do |day|
strips = 2 ** day
puts "day #{day}: #{strips} strips"
break if strips > 500
endDay 9, with 512 strips. Ranges also keep values within their limits: clamp pushes a number that is too big down to the end of the range, lifts one that is too small up to its start, and leaves all others alone. Chunky's energy bar goes from 0 to 100 – bacon can't fill it past 100, and a long run can't pull it below 0:
energy = 80
energy = (energy + 45).clamp(0..100) # bacon!
p energy
energy = (energy - 130).clamp(0..100) # a long run
energyAnd finally ranges cut pieces out of arrays and strings. In lesson 4 you wrote greeting[0, 6]: “from position 0, six characters”. With a range you say “from position … to position …” instead. Leave out the end and the piece runs to the end, and negative numbers count from the back, as always:
queue = %w[Chunky Kaz Isi Bo Mo]
p queue[0..2] # places 0 to 2
p queue[2..] # from place 2 to the end
p queue[-2..] # the last two
"Chunky Bacon"[7..]A rule of thumb: whenever you think “from … to”, write a range – to count, to ask “is it in there?”, in a case, to keep a value within its limits and to cut out a piece. Two dots include the end, three leave it out.
weekend, with a range in square brackets. And count in comfy on how many days it was between 18 and 24 degrees.temperatures = [14, 19, 23, 31, 22, 17, 26] # Monday to Sunday
# weekend = ... # Saturday and Sunday: a range in [ ]
# comfy = ... # on how many days it was 18 to 24 degrees
day days he has (1..day).sum francs. On which day does he have 100 francs or more for the first time? Walk through the days with the endless range (1..), stop with break and put the day into first_day.# first_day = ... # the first day with 100 francs or more
# walk through (1..) and stop with break