diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-07-31 07:26:46 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-07-31 07:26:46 +0900 |
| commit | facf5f99d4a37e42a54f795cee798a1c555c7cf4 (patch) | |
| tree | 0e6e5f5dcb967d3a874a4d4f442bc4147818d98c /mrbgems | |
| parent | c8fdab8725d58375ccdbd9816e48fdb021a199d4 (diff) | |
| parent | 3359b86ec7284234ae088ab682f82a0603029c34 (diff) | |
| download | mruby-facf5f99d4a37e42a54f795cee798a1c555c7cf4.tar.gz mruby-facf5f99d4a37e42a54f795cee798a1c555c7cf4.zip | |
Merge pull request #3758 from christopheraue/enumeration_perf
Improved speed of enumeration methods
Diffstat (limited to 'mrbgems')
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 16 | ||||
| -rw-r--r-- | mrbgems/mruby-enumerator/mrblib/enumerator.rb | 19 | ||||
| -rw-r--r-- | mrbgems/mruby-hash-ext/mrblib/hash.rb | 16 |
3 files changed, 26 insertions, 25 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 716eabe06..7146604d7 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -252,7 +252,7 @@ class Array # for efficiency def reverse_each(&block) - return to_enum :reverse_each unless block_given? + return to_enum :reverse_each unless block i = self.size - 1 while i>=0 @@ -474,7 +474,7 @@ class Array # scores.delete_if {|score| score < 80 } #=> [97] def delete_if(&block) - return to_enum :delete_if unless block_given? + return to_enum :delete_if unless block idx = 0 while idx < self.size do @@ -503,7 +503,7 @@ class Array # If no block is given, an Enumerator is returned instead. def reject!(&block) - return to_enum :reject! unless block_given? + return to_enum :reject! unless block len = self.size idx = 0 @@ -593,7 +593,7 @@ class Array # undefined which value is actually picked up at each iteration. def bsearch(&block) - return to_enum :bsearch unless block_given? + return to_enum :bsearch unless block if idx = bsearch_index(&block) self[idx] @@ -615,7 +615,7 @@ class Array # element itself. For more details consult the documentation for #bsearch. def bsearch_index(&block) - return to_enum :bsearch_index unless block_given? + return to_enum :bsearch_index unless block low = 0 high = size @@ -667,7 +667,7 @@ class Array # scores.delete_if {|score| score < 80 } #=> [97] def delete_if(&block) - return to_enum :delete_if unless block_given? + return to_enum :delete_if unless block idx = 0 while idx < self.size do @@ -696,7 +696,7 @@ class Array # a.keep_if { |val| val > 3 } #=> [4, 5] def keep_if(&block) - return to_enum :keep_if unless block_given? + return to_enum :keep_if unless block idx = 0 len = self.size @@ -725,7 +725,7 @@ class Array # If no block is given, an Enumerator is returned instead. def select!(&block) - return to_enum :select! unless block_given? + return to_enum :select! unless block result = [] self.each do |x| diff --git a/mrbgems/mruby-enumerator/mrblib/enumerator.rb b/mrbgems/mruby-enumerator/mrblib/enumerator.rb index a60f19aaf..c872ceedc 100644 --- a/mrbgems/mruby-enumerator/mrblib/enumerator.rb +++ b/mrbgems/mruby-enumerator/mrblib/enumerator.rb @@ -110,7 +110,7 @@ class Enumerator # p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] # def initialize(obj=nil, meth=:each, *args, &block) - if block_given? + if block obj = Generator.new(&block) else raise ArgumentError unless obj @@ -151,8 +151,9 @@ class Enumerator # # +offset+:: the starting index to use # - def with_index(offset=0) - return to_enum :with_index, offset unless block_given? + def with_index(offset=0, &block) + return to_enum :with_index, offset unless block + offset = if offset.nil? 0 elsif offset.respond_to?(:to_int) @@ -164,7 +165,7 @@ class Enumerator n = offset - 1 enumerator_block_call do |*i| n += 1 - yield i.__svalue, n + block.call i.__svalue, n end end @@ -209,11 +210,11 @@ class Enumerator # # => foo:1 # # => foo:2 # - def with_object(object) - return to_enum(:with_object, object) unless block_given? + def with_object(object, &block) + return to_enum(:with_object, object) unless block enumerator_block_call do |i| - yield [i,object] + block.call [i,object] end object end @@ -277,7 +278,7 @@ class Enumerator end obj.args = args end - return obj unless block_given? + return obj unless block enumerator_block_call(&block) end @@ -538,7 +539,7 @@ class Enumerator # just for internal class Yielder def initialize(&block) - raise LocalJumpError, "no block given" unless block_given? + raise LocalJumpError, "no block given" unless block @proc = block end diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb index 87817f833..846cba9ff 100644 --- a/mrbgems/mruby-hash-ext/mrblib/hash.rb +++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb @@ -171,7 +171,7 @@ class Hash # def delete_if(&block) - return to_enum :delete_if unless block_given? + return to_enum :delete_if unless block self.each do |k, v| self.delete(k) if block.call(k, v) @@ -228,7 +228,7 @@ class Hash # def keep_if(&block) - return to_enum :keep_if unless block_given? + return to_enum :keep_if unless block keys = [] self.each do |k, v| @@ -393,11 +393,11 @@ class Hash # # If no block is given, an enumerator is returned instead. # - def transform_keys(&b) - return to_enum :transform_keys unless block_given? + def transform_keys(&block) + return to_enum :transform_keys unless block hash = {} self.keys.each do |k| - new_key = yield(k) + new_key = block.call(k) hash[new_key] = self[k] end hash @@ -412,11 +412,11 @@ class Hash # # If no block is given, an enumerator is returned instead. # - def transform_keys!(&b) - return to_enum :transform_keys! unless block_given? + def transform_keys!(&block) + return to_enum :transform_keys! unless block self.keys.each do |k| value = self[k] - new_key = yield(k) + new_key = block.call(k) self.__delete(k) self[new_key] = value end |
