A range stores two integer values and can test a value for being a member of the range or generate a sequence of intermediate values.

It is defined in code using the range-creating operator (..):

var rg = 1..10

var start = 13
var end = 36
var rg2 = start..(end + 2)

var rg3 = new range 0, 42

Range operator priority is higher than arithmetics and even the access operator, so any expression used as range start or end should necessarily be wrapped in parens.


A range object has the following methods available:
  • int current: returns the current value in range (for iteration)
  • bool next: selects the next value as current and returns false when it runs out of values
  • void reset: resets the iteration
  • int size: returns the length of the range
  • int from: returns the start of the range
  • int to: returns the end of the range
  • string to_s: converts to string
  • int[] to_a: splats the range into an array of values

Last edited Mar 15, 2011 at 9:57 AM by impworks, version 1


No comments yet.