diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2015-10-01 22:35:06 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2015-10-01 22:35:06 +0900 |
| commit | fafe86d364c10105b83f726662f4771a4c698525 (patch) | |
| tree | 26a42b8a2de0d10ee3118960ec9a32a01c5fa621 /mrbgems/mruby-array-ext/mrblib/array.rb | |
| parent | d12926da8a7df65da47a0b8c853167ab675c53c5 (diff) | |
| download | mruby-fafe86d364c10105b83f726662f4771a4c698525.tar.gz mruby-fafe86d364c10105b83f726662f4771a4c698525.zip | |
Array#index to take block; fix #2968 close #2970
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 1f1d97376..35be79339 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -681,4 +681,32 @@ class Array return nil if self.size == result.size self.replace(result) end + + ## + # call-seq: + # ary.index(val) -> int or nil + # ary.index {|item| block } -> int or nil + # + # Returns the _index_ of the first object in +ary+ such that the object is + # <code>==</code> to +obj+. + # + # If a block is given instead of an argument, returns the _index_ of the + # first object for which the block returns +true+. Returns +nil+ if no + # match is found. + # + # ISO 15.2.12.5.14 + def index(val=NONE, &block) + return to_enum(:find_index, val) if !block && val == NONE + + if block + idx = 0 + self.each do |*e| + return idx if block.call(*e) + idx += 1 + end + else + return self.__ary_index(val) + end + nil + end end |
