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
|
File size:
1.4 KB
|
Rev | Line | |
---|
[331] | 1 | class Range
|
---|
| 2 | ##
|
---|
| 3 | # call-seq:
|
---|
| 4 | # rng.first -> obj
|
---|
| 5 | # rng.first(n) -> an_array
|
---|
| 6 | #
|
---|
| 7 | # Returns the first object in the range, or an array of the first +n+
|
---|
| 8 | # elements.
|
---|
| 9 | #
|
---|
| 10 | # (10..20).first #=> 10
|
---|
| 11 | # (10..20).first(3) #=> [10, 11, 12]
|
---|
| 12 | #
|
---|
| 13 | def first(*args)
|
---|
| 14 | return self.begin if args.empty?
|
---|
| 15 |
|
---|
| 16 | raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1)" unless args.length == 1
|
---|
| 17 | nv = args[0]
|
---|
[439] | 18 | n = nv.__to_int
|
---|
[331] | 19 | raise ArgumentError, "negative array size (or size too big)" unless 0 <= n
|
---|
| 20 | ary = []
|
---|
| 21 | each do |i|
|
---|
| 22 | break if n <= 0
|
---|
| 23 | ary.push(i)
|
---|
| 24 | n -= 1
|
---|
| 25 | end
|
---|
| 26 | ary
|
---|
| 27 | end
|
---|
[439] | 28 |
|
---|
| 29 | def max(&block)
|
---|
| 30 | val = self.first
|
---|
| 31 | last = self.last
|
---|
| 32 | return super if block
|
---|
| 33 |
|
---|
| 34 | # fast path for numerics
|
---|
| 35 | if val.kind_of?(Numeric) && last.kind_of?(Numeric)
|
---|
| 36 | raise TypeError if exclude_end? && !last.kind_of?(Fixnum)
|
---|
| 37 | return nil if val > last
|
---|
| 38 | return nil if val == last && exclude_end?
|
---|
| 39 |
|
---|
| 40 | max = last
|
---|
| 41 | max -= 1 if exclude_end?
|
---|
| 42 | return max
|
---|
| 43 | end
|
---|
| 44 |
|
---|
| 45 | # delegate to Enumerable
|
---|
| 46 | super
|
---|
| 47 | end
|
---|
| 48 |
|
---|
| 49 | def min(&block)
|
---|
| 50 | val = self.first
|
---|
| 51 | last = self.last
|
---|
| 52 | return super if block
|
---|
| 53 |
|
---|
| 54 | # fast path for numerics
|
---|
| 55 | if val.kind_of?(Numeric) && last.kind_of?(Numeric)
|
---|
| 56 | return nil if val > last
|
---|
| 57 | return nil if val == last && exclude_end?
|
---|
| 58 |
|
---|
| 59 | min = val
|
---|
| 60 | return min
|
---|
| 61 | end
|
---|
| 62 |
|
---|
| 63 | # delegate to Enumerable
|
---|
| 64 | super
|
---|
| 65 | end
|
---|
[331] | 66 | end
|
---|
Note:
See
TracBrowser
for help on using the repository browser.