mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Merge pull request #270 from dgynn/perf_tune_awesome_method_array
Additional performance tuning for Arrays
This commit is contained in:
commit
e3ab100aaf
4 changed files with 12 additions and 19 deletions
|
@ -8,7 +8,7 @@
|
||||||
# so do nothing for subsequent requires.
|
# so do nothing for subsequent requires.
|
||||||
#
|
#
|
||||||
unless defined?(AwesomePrint::Inspector)
|
unless defined?(AwesomePrint::Inspector)
|
||||||
%w(array string method object class kernel).each do |file|
|
%w(awesome_method_array string method object class kernel).each do |file|
|
||||||
require "awesome_print/core_ext/#{file}"
|
require "awesome_print/core_ext/#{file}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,31 +12,24 @@
|
||||||
#
|
#
|
||||||
# If you could think of a better way please let me know :-)
|
# If you could think of a better way please let me know :-)
|
||||||
#
|
#
|
||||||
class Array #:nodoc:
|
module AwesomeMethodArray #:nodoc:
|
||||||
|
|
||||||
alias :difference_without_awesome_print :-
|
def -(_other_ary)
|
||||||
def -(other_ary)
|
super.tap do |arr|
|
||||||
arr = difference_without_awesome_print(other_ary)
|
|
||||||
if self.instance_variable_defined?(:@__awesome_methods__)
|
|
||||||
arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__))
|
arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__))
|
||||||
end
|
end
|
||||||
arr
|
|
||||||
end
|
end
|
||||||
|
|
||||||
alias :intersection_without_awesome_print :&
|
def &(_other_ary)
|
||||||
def &(other_ary)
|
super.tap do |arr|
|
||||||
arr = intersection_without_awesome_print(other_ary)
|
|
||||||
if self.instance_variable_defined?(:@__awesome_methods__)
|
|
||||||
arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__))
|
arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__))
|
||||||
end
|
end
|
||||||
arr
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
# Intercepting Array#grep needs a special treatment since grep accepts
|
# Intercepting Array#grep needs a special treatment since grep accepts
|
||||||
# an optional block.
|
# an optional block.
|
||||||
#
|
#
|
||||||
alias :original_grep :grep
|
|
||||||
def grep(pattern, &blk)
|
def grep(pattern, &blk)
|
||||||
#
|
#
|
||||||
# The following looks rather insane and I've sent numerous hours trying
|
# The following looks rather insane and I've sent numerous hours trying
|
||||||
|
@ -62,9 +55,9 @@ class Array #:nodoc:
|
||||||
# the comment :-)
|
# the comment :-)
|
||||||
#
|
#
|
||||||
arr = unless blk
|
arr = unless blk
|
||||||
original_grep(pattern)
|
super(pattern)
|
||||||
else
|
else
|
||||||
original_grep(pattern) do |match|
|
super(pattern) do |match|
|
||||||
#
|
#
|
||||||
# The binding can only be used with Ruby-defined methods, therefore
|
# The binding can only be used with Ruby-defined methods, therefore
|
||||||
# we must rescue potential "ArgumentError: Can't create Binding from
|
# we must rescue potential "ArgumentError: Can't create Binding from
|
||||||
|
@ -79,10 +72,8 @@ class Array #:nodoc:
|
||||||
yield match
|
yield match
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if self.instance_variable_defined?(:@__awesome_methods__)
|
arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__))
|
||||||
arr.instance_variable_set(:@__awesome_methods__, self.instance_variable_get(:@__awesome_methods__))
|
arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap.
|
||||||
arr.reject! { |item| !(item.is_a?(Symbol) || item.is_a?(String)) } # grep block might return crap.
|
|
||||||
end
|
|
||||||
arr
|
arr
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -16,6 +16,7 @@ class Class #:nodoc:
|
||||||
define_method name do |*args|
|
define_method name do |*args|
|
||||||
methods = original_method.bind(self).call(*args)
|
methods = original_method.bind(self).call(*args)
|
||||||
methods.instance_variable_set(:@__awesome_methods__, self)
|
methods.instance_variable_set(:@__awesome_methods__, self)
|
||||||
|
methods.extend(AwesomeMethodArray)
|
||||||
methods
|
methods
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,6 +16,7 @@ class Object #:nodoc:
|
||||||
define_method name do |*args|
|
define_method name do |*args|
|
||||||
methods = original_method.bind(self).call(*args)
|
methods = original_method.bind(self).call(*args)
|
||||||
methods.instance_variable_set(:@__awesome_methods__, self)
|
methods.instance_variable_set(:@__awesome_methods__, self)
|
||||||
|
methods.extend(AwesomeMethodArray)
|
||||||
methods
|
methods
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue