What is a Range in Ruby and how is it used?

Last updated 27 June 2026

A Range represents an interval between a start and end value, created with .. (inclusive) or ... (exclusive of the end). It works with any objects that are comparable and can be sequenced.

  • Two literal forms: 1..5 includes 5; 1...5 stops at 4.

  • Common uses:

    • Iteration: (1..5).each.

    • Membership tests: (1..10).include?(7) or cover?.

    • Slicing: array[2..4], "hello"[0..1].

    • As a case condition: when 1..10.

  • Works beyond integers: Any objects supporting <=> (and succ for iteration), e.g. ('a'..'e') or dates.

  • Endless and beginless ranges: (1..) and (..10) are valid and handy for slicing and comparisons.

  • https://docs.ruby-lang.org/en/3.2/Range.html
  • https://docs.ruby-lang.org/en/master/Range.html
  • https://www.geeksforgeeks.org/ruby/ruby-range-class-methods/
  • https://thoughtbot.com/blog/custom-ranges-in-ruby
  • https://hyring.com/jobseeker-toolkit/interview-questions/technical/ruby
  • https://medium.com/@german.gimenez.silva/understanding-rubys-range-operator-in-activerecord-queries-ca8020e2b865
  • https://www.tutorialspoint.com/ruby/ruby_ranges.htm
  • https://medium.com/@rarunkumar/elevating-your-ruby-code-a-guide-to-choosing-the-right-methods-9ef10814951b
All Ruby interview questions