Class: Object
- Inherits:
- BasicObject
- Includes:
- InstanceExecMethods
- Defined in:
- lib/ruco/core_ext/object.rb,
lib/ruco/core_ext/object.rb,
lib/ruco/core_ext/object.rb,
lib/ruco/core_ext/object.rb,
lib/ruco/core_ext/object.rb
Defined Under Namespace
Modules: InstanceExecMethods
Instance Method Summary collapse
-
#cmemoize(*method_names) ⇒ Object
Memoize class methods.
- #deep_copy ⇒ Object
-
#delegate(*methods) ⇒ Object
copy from active_support.
-
#instance_exec(*args, &block) ⇒ Object
Evaluate the block with the given arguments within the context of this object, so self is set to the method receiver.
- #memoize(*names) ⇒ Object
-
#silence_warnings ⇒ Object
copy from active_support.
-
#with_warnings(flag) ⇒ Object
copy from active_support.
Instance Method Details
#cmemoize(*method_names) ⇒ Object
Memoize class methods
100 101 102 103 104 |
# File 'lib/ruco/core_ext/object.rb', line 100 def cmemoize(*method_names) (class << self; self; end).class_eval do memoize(*method_names) end end |
#deep_copy ⇒ Object
49 50 51 |
# File 'lib/ruco/core_ext/object.rb', line 49 def deep_copy Marshal.load(Marshal.dump(self)) end |
#delegate(*methods) ⇒ Object
copy from active_support
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/ruco/core_ext/object.rb', line 3 def delegate(*methods) = methods.pop unless .is_a?(Hash) && to = [:to] raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)." end methods.each do |method| module_eval(<<-EOS, "(__DELEGATION__)", 1) def #{method}(*args, &block) #{to}.__send__(#{method.inspect}, *args, &block) end EOS end end |
#instance_exec(*args, &block) ⇒ Object
Evaluate the block with the given arguments within the context of this object, so self is set to the method receiver.
From Mauricio’s eigenclass.org/hiki/bounded+space+instance_exec
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruco/core_ext/object.rb', line 29 def instance_exec(*args, &block) begin old_critical, Thread.critical = Thread.critical, true n = 0 n += 1 while respond_to?(method_name = "__instance_exec#{n}") InstanceExecMethods.module_eval { define_method(method_name, &block) } ensure Thread.critical = old_critical end begin send(method_name, *args) ensure InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil end end |
#memoize(*names) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ruco/core_ext/object.rb', line 81 def memoize(*names) names.each do |name| unmemoized = "__unmemoized_#{name}" class_eval %{ alias :#{unmemoized} :#{name} private :#{unmemoized} def #{name}(*args) cache = (@#{unmemoized} ||= {}) if cache.has_key?(args) cache[args] else cache[args] = send(:#{unmemoized}, *args).freeze end end } end end |
#silence_warnings ⇒ Object
copy from active_support
56 57 58 |
# File 'lib/ruco/core_ext/object.rb', line 56 def silence_warnings with_warnings(nil) { yield } end |
#with_warnings(flag) ⇒ Object
copy from active_support
61 62 63 64 65 66 |
# File 'lib/ruco/core_ext/object.rb', line 61 def with_warnings(flag) old_verbose, $VERBOSE = $VERBOSE, flag yield ensure $VERBOSE = old_verbose end |