source: EcnlProtoTool/trunk/mruby-1.2.0/mrblib/range.rb@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby
File size: 967 bytes
Line 
1##
2# Range
3#
4# ISO 15.2.14
5class Range
6
7 ##
8 # Calls the given block for each element of +self+
9 # and pass the respective element.
10 #
11 # ISO 15.2.14.4.4
12 def each(&block)
13 return to_enum :each unless block_given?
14
15 val = self.first
16 last = self.last
17
18 if val.kind_of?(Fixnum) && last.kind_of?(Fixnum) # fixnums are special
19 lim = last
20 lim += 1 unless exclude_end?
21 i = val
22 while i < lim
23 block.call(i)
24 i += 1
25 end
26 return self
27 end
28
29 raise TypeError, "can't iterate" unless val.respond_to? :succ
30
31 return self if (val <=> last) > 0
32
33 while (val <=> last) < 0
34 block.call(val)
35 val = val.succ
36 end
37
38 block.call(val) if !exclude_end? && (val <=> last) == 0
39 self
40 end
41
42 # redefine #hash 15.3.1.3.15
43 def hash
44 h = first.hash ^ last.hash
45 h += 1 if self.exclude_end?
46 h
47 end
48end
49
50##
51# Range is enumerable
52#
53# ISO 15.2.14.3
54class Range
55 include Enumerable
56end
Note: See TracBrowser for help on using the repository browser.