lib/mack-facets/extensions/kernel.rb in mack-facets-0.7.1.1 vs lib/mack-facets/extensions/kernel.rb in mack-facets-0.8.0
- old
+ new
@@ -1,9 +1,55 @@
require 'pp'
require 'stringio'
module Kernel
+ # Aliases an instance method to a new name. It will only do the aliasing once, to prevent
+ # issues with reloading a class and causing a StackLevel too deep error.
+ # The method takes two arguments, the first is the original name of the method, the second,
+ # optional, parameter is the new name of the method. If you don't specify a new method name
+ # it will be generated with _original_<original_name>.
+ #
+ # Example:
+ # class Popcorn < Corn
+ # alias_instance_method :poppy
+ # alias_instance_method :corny, :old_corny
+ # def poppy
+ # 2 * _original_poppy
+ # end
+ # def corny
+ # 'pop' + old_corny
+ # end
+ # end
+ def alias_instance_method(orig_name, new_name = "_original_#{orig_name}")
+ alias_method new_name.to_sym, orig_name.to_sym unless method_defined?(new_name.to_s)
+ end
+
+ # Aliases a class method to a new name. It will only do the aliasing once, to prevent
+ # issues with reloading a class and causing a StackLevel too deep error.
+ # The method takes two arguments, the first is the original name of the method, the second,
+ # optional, parameter is the new name of the method. If you don't specify a new method name
+ # it will be generated with _original_<original_name>.
+ #
+ # Example:
+ # class President
+ # alias_class_method :good
+ # alias_class_method :bad, :old_bad
+ # def self.good
+ # 'Bill ' + _original_good
+ # end
+ # def self.bad
+ # "Either #{old_bad}"
+ # end
+ # end
+ def alias_class_method(orig_name, new_name = "_original_#{orig_name}")
+ eval(%{
+ class << self
+ alias_method :#{new_name}, :#{orig_name} unless method_defined?("#{new_name}")
+ end
+ })
+ end
+
def pp_to_s(object)
pp_out = StringIO.new
PP.pp(object,pp_out)
return pp_out.string
end