source: EcnlProtoTool/trunk/mruby-2.1.1/mrblib/range.rb@ 439

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

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 1.2 KB
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
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 if val.kind_of?(String) && last.kind_of?(String) # strings are special
30 if val.respond_to? :upto
31 return val.upto(last, exclude_end?, &block)
32 else
33 str_each = true
34 end
35 end
36
37 raise TypeError, "can't iterate" unless val.respond_to? :succ
38
39 return self if (val <=> last) > 0
40
41 while (val <=> last) < 0
42 block.call(val)
43 val = val.succ
44 if str_each
45 break if val.size > last.size
46 end
47 end
48
49 block.call(val) if !exclude_end? && (val <=> last) == 0
50 self
51 end
52
53 # redefine #hash 15.3.1.3.15
54 def hash
55 h = first.hash ^ last.hash
56 h += 1 if self.exclude_end?
57 h
58 end
59end
60
61##
62# Range is enumerable
63#
64# ISO 15.2.14.3
65class Range
66 include Enumerable
67end
Note: See TracBrowser for help on using the repository browser.