Class: Range

Inherits:
Object show all
Defined in:
lib/source/ruby.rb

Overview

A Range represents an interval – a set of values with a start and an end. Ranges may be constructed using the s..e and s...e literals, or with Range::new. Ranges constructed using .. run from the start to the end inclusively. Those created using ... exclude the end value. When used as an iterator, ranges return each value in the sequence.

(-1..-5).to_a      #=> []
(-5..-1).to_a      #=> [-5, -4, -3, -2, -1]
('a'..'e').to_a    #=> ["a", "b", "c", "d", "e"]
('a'...'e').to_a   #=> ["a", "b", "c", "d"]

Ranges can be constructed using objects of any type, as long as the <=> comparison operator and the succ method (to return the next object in sequence) are defined.

class XChain
  attr :length

  def initialize(n)
    @length = n
  end

  def succ
    one_more = @length + 1
    XChain.new(one_more)
  end

  def <=>(other)
    @length <=> other.length
  end

  def inspect
    'x' * @length
  end
end

r = XChain.new(2)..XChain.new(5)    #=> xx..xxxxx
r.to_a                              #=> [xx, xxx, xxxx, xxxxx]
r.member? XChain.new(4)             #=> true

Instance Method Summary collapse

Constructor Details

#initialize(start, finish, exclusive = false) ⇒ Range

call-seq:

Range.new(start, end, exclusive=false) -> range

Constructs a range using the given start and end. If exclusive is omitted or is false, the range will include the end object; otherwise, it will be excluded.



4844
4845
4846
4847
4848
# File 'lib/source/ruby.rb', line 4844

def initialize(start,finish,exclusive=false)
  `this.__start__=start`
  `this.__end__=finish`
  `this.__exclusive__=exclusive`
end

Instance Method Details

#==(object) ⇒ Object

call-seq:

rng == obj -> true or false

Returns true only if obj is a Range, has equivalent beginning and end items (by comparing them with ==), and has the same exclude_end? setting as rng.

(0..2) == (0..2)            #=> true
(0..2) == Range.new(0,2)    #=> true
(0..2) == (0...2)           #=> false


4861
4862
4863
4864
# File 'lib/source/ruby.rb', line 4861

def ==(object)
  `if(object.constructor!==c$Range){return false;}`
  `this.__start__.m$_eql2(object.__start__)&&this.__end__.m$_eql2(object.__end__)&&this.__exclusive__==object.__exclusive__`
end

#===(obj) ⇒ Object

call-seq:

rng === obj       -> true or false
rng.include?(obj) -> true or false
rng.member?(obj)  -> true or false

Returns true if obj is an element of rng, false otherwise. Conveniently, === is the comparison operator used by case statements.

case 79
  when 1..50   : puts "low"
  when 51..75  : puts "medium"
  when 76..100 : puts "high"
end

produces:

high


4885
4886
4887
4888
# File 'lib/source/ruby.rb', line 4885

def ===(obj)
  `var s=#{obj <=> `this.__start__`},e=#{obj <=> `this.__end__`}`
  `s==0||s==1?(this.__exclusive__?e==-1:e==-1||e==0):false`
end

#beginObject

call-seq:

rng.begin -> obj
rng.first -> obj

Returns the first object in rng.

(1..10).begin   #=> 1


4898
4899
4900
# File 'lib/source/ruby.rb', line 4898

def begin
  `this.__start__`
end

#eachObject

FIX: Incomplete



4903
4904
4905
4906
4907
# File 'lib/source/ruby.rb', line 4903

def each
  `var start=this.__start__,end=this.__end__`
  `if(typeof(start)=='number'&&typeof(end)=='number'){if(!this.__exclusive__){end++;};for(var i=start;i<end;i++){try{#{yield `i`};}catch(e){switch(e.__keyword__){case 'next':break;case 'break':return e.__return__;break;case 'redo':--i;break;default:throw(e);};};};}`
  return self
end

#endObject

call-seq:

rng.end  -> obj
rng.last -> obj

Returns the object that defines the end of rng.

(1..10).end     #=> 10
(1...10).end    #=> 10


4918
4919
4920
# File 'lib/source/ruby.rb', line 4918

def end()
  `this.__end__`
end

#eql?(object) ⇒ Boolean

call-seq:

rng.eql?(obj) -> true or false

Returns true only if obj is a Range, has equivalent beginning and end items (by comparing them with eql?), and has the same exclude_end? setting as rng.

(0..2).eql?(0..2)             #=> true
(0..2).eql? Range.new(0,2)    #=> true
(0..2).eql?(0...2)            #=> false

Returns:

  • (Boolean)


4942
4943
4944
4945
# File 'lib/source/ruby.rb', line 4942

def eql?(object)
  `if(object.constructor!==c$Range){return false;}`
  `this.__start__.m$eql_bool(object.__start__)&&this.__end__.m$eql_bool(object.__end__)&&this.__exclusive__==object.__exclusive__`
end

#exclude_end?Boolean

call-seq:

rng.exclude_end? -> true or false

Returns true if rng excludes its end value.

Returns:

  • (Boolean)


4927
4928
4929
# File 'lib/source/ruby.rb', line 4927

def exclude_end?
  `this.__exclusive__`
end

#firstObject

call-seq:

rng.begin -> obj
rng.first -> obj

Returns the first object in rng.

(1..10).first   #=> 1


4955
4956
4957
# File 'lib/source/ruby.rb', line 4955

def first
  `this.__start__`
end

#hashObject

FIX: Incomplete



4960
4961
# File 'lib/source/ruby.rb', line 4960

def hash # :nodoc:
end

#include?(obj) ⇒ Boolean

call-seq:

rng === obj       -> true or false
rng.include?(obj) -> true or false
rng.member?(obj)  -> true or false

Returns true if obj is an element of rng, false otherwise.

(1..10).include?(10)    #=> true
(1...10).include?(10)   #=> false

Returns:

  • (Boolean)


4973
4974
4975
4976
# File 'lib/source/ruby.rb', line 4973

def include?(obj)
  `var s=#{obj <=> `this.__start__`},e=#{obj <=> `this.__end__`}`
  `s==0||s==1?(this.__exclusive__?e==-1:e==-1||e==0):false`
end

#inspectObject

call-seq:

rng.inspect -> string

Converts rng to a printable form (using inspect to convert the start and end objects).



4984
4985
4986
# File 'lib/source/ruby.rb', line 4984

def inspect
  `$q(''+this.__start__.m$inspect()+(this.__exclusive__?'...':'..')+this.__end__.m$inspect())`
end

#lastObject

call-seq:

rng.end  -> obj
rng.last -> obj

Returns the object that defines the end of rng.

(1..10).last    #=> 10
(1...10).last   #=> 10


4997
4998
4999
# File 'lib/source/ruby.rb', line 4997

def last
  `this.__end__`
end

#member?(obj) ⇒ Boolean

call-seq:

rng === obj       -> true or false
rng.include?(obj) -> true or false
rng.member?(obj)  -> true or false

Returns true if obj is an element of rng, false otherwise.

(1..10).member?(10)    #=> true
(1...10).member?(10)   #=> false

Returns:

  • (Boolean)


5011
5012
5013
5014
# File 'lib/source/ruby.rb', line 5011

def member?(obj)
  `var s=#{obj <=> `this.__start__`},e=#{obj <=> `this.__end__`}`
  `s==0||s==1?(this.__exclusive__?e==-1:e==-1||e==0):false`
end

#stepObject

FIX: Incomplete



5017
5018
# File 'lib/source/ruby.rb', line 5017

def step
end

#to_sObject

call-seq:

rng.to_s -> string

Converts rng to a printable form.



5025
5026
5027
# File 'lib/source/ruby.rb', line 5025

def to_s
  `$q(''+this.__start__+(this.__exclusive__?'...':'..')+this.__end__)`
end