diff options
author | Jeremy Evans <[email protected]> | 2019-05-11 16:32:00 -0700 |
---|---|---|
committer | Jeremy Evans <[email protected]> | 2019-05-30 18:34:45 -0700 |
commit | 1cd93f1cdfbe6f7e71b05b3f8e707f21d70e94ba (patch) | |
tree | a1741805f80749f90f35d21f0f646148b41dd610 /lib/delegate.rb | |
parent | 856593cc4972562d2ab0a59a61f38fe3a4a863ab (diff) |
Allow DelegateClass() to module_eval given block
Methods that return classes often module_eval the given block
(e.g. Class.new and Struct.new). This allows DelegateClass to
work similarly. This makes it easier to use DelegateClass
directly without subclassing, so as not to create an unnecessary
subclass.
Implements [Feature #15842]
Diffstat (limited to 'lib/delegate.rb')
-rw-r--r-- | lib/delegate.rb | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/delegate.rb b/lib/delegate.rb index 9f8ef9d5ad..859cc2ed84 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -360,6 +360,14 @@ end # end # end # +# or: +# +# MyClass = DelegateClass(ClassToDelegateTo) do # Step 1 +# def initialize +# super(obj_of_ClassToDelegateTo) # Step 2 +# end +# end +# # Here's a sample of use from Tempfile which is really a File object with a # few special rules about storage location and when the File should be # deleted. That makes for an almost textbook perfect example of how to use @@ -383,7 +391,7 @@ end # # ... # end # -def DelegateClass(superclass) +def DelegateClass(superclass, &block) klass = Class.new(Delegator) methods = superclass.instance_methods methods -= ::Delegator.public_api @@ -410,5 +418,6 @@ def DelegateClass(superclass) klass.define_singleton_method :protected_instance_methods do |all=true| super(all) | superclass.protected_instance_methods end + klass.module_eval(&block) if block return klass end |