Class: Range
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
-
#==(object) ⇒ Object
call-seq: rng == obj -> true or false.
-
#===(obj) ⇒ Object
call-seq: rng === obj -> true or false rng.include?(obj) -> true or false rng.member?(obj) -> true or false.
-
#begin ⇒ Object
call-seq: rng.begin -> obj rng.first -> obj.
-
#each ⇒ Object
FIX: Incomplete.
-
#end ⇒ Object
call-seq: rng.end -> obj rng.last -> obj.
-
#eql?(object) ⇒ Boolean
call-seq: rng.eql?(obj) -> true or false.
-
#exclude_end? ⇒ Boolean
call-seq: rng.exclude_end? -> true or false.
-
#first ⇒ Object
call-seq: rng.begin -> obj rng.first -> obj.
-
#hash ⇒ Object
FIX: Incomplete.
-
#include?(obj) ⇒ Boolean
call-seq: rng === obj -> true or false rng.include?(obj) -> true or false rng.member?(obj) -> true or false.
-
#initialize(start, finish, exclusive = false) ⇒ Range
constructor
call-seq: Range.new(start, end, exclusive=false) -> range.
-
#inspect ⇒ Object
call-seq: rng.inspect -> string.
-
#last ⇒ Object
call-seq: rng.end -> obj rng.last -> obj.
-
#member?(obj) ⇒ Boolean
call-seq: rng === obj -> true or false rng.include?(obj) -> true or false rng.member?(obj) -> true or false.
-
#step ⇒ Object
FIX: Incomplete.
-
#to_s ⇒ Object
call-seq: rng.to_s -> string.
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.
4806 4807 4808 4809 4810 |
# File 'lib/source/ruby.rb', line 4806 def initialize(start,finish,exclusive=false) `this._start=start` `this._end=finish` `this._exclusive=exclusive` end |
Instance Method Details
#==(object) ⇒ Object
4823 4824 4825 4826 |
# File 'lib/source/ruby.rb', line 4823 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
4847 4848 4849 4850 |
# File 'lib/source/ruby.rb', line 4847 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 |
#begin ⇒ Object
call-seq:
rng.begin -> obj
rng.first -> obj
Returns the first object in rng.
(1..10).begin #=> 1
4860 4861 4862 |
# File 'lib/source/ruby.rb', line 4860 def begin `this._start` end |
#each ⇒ Object
FIX: Incomplete
4865 4866 4867 4868 4869 |
# File 'lib/source/ruby.rb', line 4865 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++){#{yield `i`};};}` return self end |
#end ⇒ Object
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
4880 4881 4882 |
# File 'lib/source/ruby.rb', line 4880 def end() `this._end` end |
#eql?(object) ⇒ Boolean
4904 4905 4906 4907 |
# File 'lib/source/ruby.rb', line 4904 def eql?(object) `if(object.constructor!==c$Range){return false;}` `this._start.m$eqlBool(object._start)&&this._end.m$eqlBool(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.
4889 4890 4891 |
# File 'lib/source/ruby.rb', line 4889 def exclude_end? `this._exclusive` end |
#first ⇒ Object
call-seq:
rng.begin -> obj
rng.first -> obj
Returns the first object in rng.
(1..10).first #=> 1
4917 4918 4919 |
# File 'lib/source/ruby.rb', line 4917 def first `this._start` end |
#hash ⇒ Object
FIX: Incomplete
4922 4923 |
# File 'lib/source/ruby.rb', line 4922 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
4935 4936 4937 4938 |
# File 'lib/source/ruby.rb', line 4935 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 |
#inspect ⇒ Object
call-seq:
rng.inspect -> string
Converts rng to a printable form (using inspect
to convert the start and end objects).
4946 4947 4948 |
# File 'lib/source/ruby.rb', line 4946 def inspect `$q(''+this._start.m$inspect()+(this._exclusive?'...':'..')+this._end.m$inspect())` end |
#last ⇒ Object
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
4959 4960 4961 |
# File 'lib/source/ruby.rb', line 4959 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
4973 4974 4975 4976 |
# File 'lib/source/ruby.rb', line 4973 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 |
#to_s ⇒ Object
call-seq:
rng.to_s -> string
Converts rng to a printable form.
4987 4988 4989 |
# File 'lib/source/ruby.rb', line 4987 def to_s `$q(''+this._start+(this._exclusive?'...':'..')+this._end)` end |