Class: Range
- Includes:
- Enumerable
- Defined in:
- ext/enterprise_script_service/mruby/mrblib/range.rb,
ext/enterprise_script_service/mruby/mrblib/range.rb,
ext/enterprise_script_service/mruby/mrbgems/mruby-range-ext/mrblib/range.rb
Overview
Range is enumerable
ISO 15.2.14.3
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.
Methods included from Enumerable
#+, #all?, #any?, #chain, #collect, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_slice, #each_with_index, #each_with_object, #entries, #find_all, #find_index, #flat_map, #grep, #group_by, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #sort, #sort_by, #take, #take_while, to_h, #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
12 13 14 15 16 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 |
# File 'ext/enterprise_script_service/mruby/mrblib/range.rb', line 12 def each(&block) return to_enum :each unless block val = self.first last = self.last if val.kind_of?(Fixnum) && last.kind_of?(Fixnum) # fixnums 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) # fixnums 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 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-range-ext/mrblib/range.rb', line 13 def first(*args) 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
54 55 56 57 58 |
# File 'ext/enterprise_script_service/mruby/mrblib/range.rb', line 54 def hash h = first.hash ^ last.hash h += 1 if self.exclude_end? h end |