Lesson 11 of 23 · Ruby 3.4
Check a numeric interval
Describe numeric boundaries with ranges and test whether a value lies inside them.
Learn
A Range describes values between two endpoints. In a bounded numeric range, .. includes the end and ... excludes it. Both forms include the beginning. Parentheses make a range clear when you call a method directly on it.
include?(value) tests membership; cover?(value) tests whether a value falls within the boundaries. For the ascending integer ranges and integer candidates used here, both checks agree. A range is not an array that stores every value between its endpoints.
Example
allowed = 1..5
p allowed.include?(3)
p allowed.include?(5)
p (1...5).include?(5)Experiment
Predict whether the rating is accepted. Try 0 and 5, then change .. to ... to investigate the upper boundary.
The closing hour should be excluded. Predict the two checks, then change closing time from 17 to 18.