Class: Range
- Inherits:
-
Object
- Object
- Range
- Includes:
- Enumerable
- Defined in:
- mrblib/range.rb,
mrbgems/mruby-range-ext/mrblib/range.rb
Overview
Range
ISO 15.2.14
Constant Summary
Constants included from Enumerable
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Calls the given block for each element of
selfand pass the respective element. -
#first(*args) ⇒ Object
call-seq: rng.first -> obj rng.first(n) -> an_array.
-
#hash ⇒ Object
redefine #hash 15.3.1.3.15.
-
#last(*args) ⇒ Object
call-seq: rng.last -> obj rng.last(n) -> an_array.
- #max(&block) ⇒ Object
- #min(&block) ⇒ Object
-
#overlap?(other) ⇒ Boolean
Compare two ranges and see if they overlap each other (1..5).overlap?(4..6) # => true (1..5).overlap?(7..9) # => false.
-
#to_a ⇒ Object
(also: #entries)
call-seq: rng.to_a -> array rng.entries -> array.
Methods included from Enumerable
#all?, #any?, #chain, #chunk, #chunk_while, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #filter_map, #find_all, #find_index, #flat_map, #grep, #grep_v, #group_by, #include?, #inject, #lazy, #max_by, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_h, #uniq, #zip
Instance Method Details
#each(&block) ⇒ Object
Calls the given block for each element of self
and pass the respective element.
ISO 15.2.14.4.4
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'mrblib/range.rb', line 17 def each(&block) return to_enum :each unless block val = self.begin last = self.end if val.kind_of?(Integer) && last.nil? i = val while true block.call(i) i += 1 end return self end if val.kind_of?(String) && last.nil? if val.respond_to? :__upto_endless return val.__upto_endless(&block) else str_each = true end end if val.kind_of?(Integer) && last.kind_of?(Integer) # integers are special lim = last lim += 1 unless exclude_end? i = val while i < lim block.call(i) i += 1 end return self end if val.kind_of?(String) && last.kind_of?(String) # strings are special if val.respond_to? :upto return val.upto(last, exclude_end?, &block) else str_each = true end end raise TypeError, "can't iterate" unless val.respond_to? :succ return self if (val <=> last) > 0 while (val <=> last) < 0 block.call(val) val = val.succ if str_each break if val.size > last.size end end block.call(val) if !exclude_end? && (val <=> last) == 0 self end |
#first(*args) ⇒ Object
call-seq: rng.first -> obj rng.first(n) -> an_array
Returns the first object in the range, or an array of the first n
elements.
(10..20).first #=> 10
(10..20).first(3) #=> [10, 11, 12]
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'mrbgems/mruby-range-ext/mrblib/range.rb', line 13 def first(*args) raise RangeError, "cannot get the first element of beginless range" if self.begin.nil? return self.begin if args.empty? raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1)" unless args.length == 1 nv = args[0] n = nv.__to_int raise ArgumentError, "negative array size (or size too big)" unless 0 <= n ary = [] each do |i| break if n <= 0 ary.push(i) n -= 1 end ary end |
#hash ⇒ Object
redefine #hash 15.3.1.3.15
76 77 78 79 80 |
# File 'mrblib/range.rb', line 76 def hash h = first.hash ^ last.hash h += 1 if self.exclude_end? h end |
#last(*args) ⇒ Object
call-seq: rng.last -> obj rng.last(n) -> an_array
Returns the last object in the range,
or an array of the last n elements.
Note that with no arguments last will return the object that defines
the end of the range even if #exclude_end? is true.
(10..20).last #=> 20
(10...20).last #=> 20
(10..20).last(3) #=> [18, 19, 20]
(10...20).last(3) #=> [17, 18, 19]
45 46 47 48 49 50 51 52 53 54 |
# File 'mrbgems/mruby-range-ext/mrblib/range.rb', line 45 def last(*args) raise RangeError, "cannot get the last element of endless range" if self.end.nil? return self.end if args.empty? raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1)" unless args.length == 1 nv = args[0] n = nv.__to_int raise ArgumentError, "negative array size (or size too big)" unless 0 <= n return self.to_a.last(nv) end |
#max(&block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'mrbgems/mruby-range-ext/mrblib/range.rb', line 56 def max(&block) val = self.begin last = self.end return super(&block) if block raise RangeError, "cannot get the maximum of endless range" if last.nil? # fast path for numerics if val.kind_of?(Numeric) && last.kind_of?(Numeric) raise TypeError if exclude_end? && !last.kind_of?(Integer) return nil if val > last return nil if val == last && exclude_end? max = last max -= 1 if exclude_end? return max end # delegate to Enumerable super() end |
#min(&block) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'mrbgems/mruby-range-ext/mrblib/range.rb', line 78 def min(&block) val = self.begin last = self.end if block raise RangeError, "cannot get the minimum of endless range with custom comparison method" if last.nil? return super(&block) end return val if last.nil? # fast path for numerics if val.kind_of?(Numeric) && last.kind_of?(Numeric) return nil if val > last return nil if val == last && exclude_end? min = val return min end # delegate to Enumerable super() end |
#overlap?(other) ⇒ Boolean
Compare two ranges and see if they overlap each other (1..5).overlap?(4..6) # => true (1..5).overlap?(7..9) # => false
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'mrbgems/mruby-range-ext/mrblib/range.rb', line 103 def overlap?(other) raise TypeError, "argument must be a range" unless other.kind_of?(Range) self_begin = self.begin other_end = other.end other_excl = other.exclude_end? return false if __empty_range?(self_begin, other_end, other_excl) other_begin = other.begin self_end = self.end self_excl = self.exclude_end? return false if __empty_range?(other_begin, self_end, self_excl) return true if self_begin == other_begin return false if __empty_range?(self_begin, self_end, self_excl) return false if __empty_range?(other_begin, other_end, other_excl) true end |
#to_a ⇒ Object Also known as: entries
call-seq: rng.to_a -> array rng.entries -> array
Returns an array containing the items in the range.
(1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7]
(1..).to_a #=> RangeError: cannot convert endless range to an array
91 92 93 94 95 |
# File 'mrblib/range.rb', line 91 def to_a a = __num_to_a return a if a super end |