Ranges

Contents
Two and three dots
Include checks
Loop over ranges
Ranges in case

Two and three dots

Two dots build a range that includes its end, while three dots build one that excludes it. Ranges also cover strings in order, such as letters from a to c. Small ranges expand into arrays with to_a for a quick look.

puts (1..5).to_a.length puts (1...5).to_a.length puts ("a".."c").to_a.length

5
4
3

Include checks

The predicate include? reports whether a value sits inside the range. The predicate cover? answers the same question with a single comparison, which runs faster on very large ranges.

r = (1..5) puts r.include?(3) puts r.include?(9) puts r.cover?(3)

true
false
true

Loop over ranges

Ranges respond to each just like arrays, yielding every value in turn. The method step moves by more than one, which is handy for skipping ahead. Call to_a when an actual array is needed downstream.

(1..3).each do |n| puts n end puts (1..10).step(3).to_a.length

1
2
3
4

The stepped range holds 1, 4, 7, and 10, so its length is 4.

Ranges in case

A when branch compares with triple equals, so a range matches any value inside it. Number bands, such as school grades, read naturally this way. The first matching branch wins, with else as the fallback.

score = 85 result = case score when 90..100 "top" when 75..89 "good" else "rest" end puts result

good

Next: Methods

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: