| Module | Kernel |
| In: |
lib/mack-facets/extensions/kernel.rb
|
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
# File lib/mack-facets/extensions/kernel.rb, line 53
53: def alias_class_method(orig_name, new_name = "_original_#{orig_name}")
54: eval(%{
55: class << self
56: alias_method :#{new_name}, :#{orig_name} unless method_defined?("#{new_name}")
57: end
58: })
59: end
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
# File lib/mack-facets/extensions/kernel.rb, line 32
32: def alias_instance_method(orig_name, new_name = "_original_#{orig_name}")
33: alias_method new_name.to_sym, orig_name.to_sym unless method_defined?(new_name.to_s)
34: end
# File lib/mack-facets/extensions/kernel.rb, line 61
61: def pp_to_s(object)
62: pp_out = StringIO.new
63: PP.pp(object,pp_out)
64: return pp_out.string
65: end
# File lib/mack-facets/extensions/kernel.rb, line 67
67: def retryable(options = {}, &block)
68: opts = { :tries => 1, :on => Exception }.merge(options)
69:
70: retries = opts[:tries]
71: retry_exceptions = [opts[:on]].flatten
72:
73: x = %{
74: begin
75: return yield
76: rescue #{retry_exceptions.join(", ")} => e
77: retries -= 1
78: if retries > 0
79: retry
80: else
81: raise e
82: end
83: end
84: }
85:
86: eval(x, &block)
87: end
Returns true/false if the current version of Ruby equals the specified version
# File lib/mack-facets/extensions/kernel.rb, line 90
90: def ruby?(v)
91: RUBY_VERSION == v
92: end
# File lib/mack-facets/extensions/kernel.rb, line 6
6: def run_once
7: path = File.expand_path(caller.first)
8: unless ($__already_run_block ||= []).include?(path)
9: yield
10: $__already_run_block << path
11: end
12: # puts "$__already_run_block: #{$__already_run_block.inspect}"
13: end