Module: Instantise
- Defined in:
- lib/more/facets/instantise.rb
Overview
Instantise
Instantise converts a module’s class methods into instance methods such that the first parameter is passed self at the instance level. This promotes DRY programming when wishing to offer both an inheritable and a module callable procedure.
Usage
module MyModule
extend Instantise
def self.jumble( obj, arg )
obj + arg
end
end
class String
include MyModule
end
MyModule.jumble( "Try", "Me" ) #=> "TryMe"
"Try".jumble( "Me" ) #=> 'TryMe'
Instance Method Summary collapse
-
#singleton_method_added(meth) ⇒ Object
alias_method :singleton_method_added_promoteself, :singleton_method_added if defined?(singleton_method_added).
Instance Method Details
#singleton_method_added(meth) ⇒ Object
alias_method :singleton_method_added_promoteself, :singleton_method_added if defined?(singleton_method_added)
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/more/facets/instantise.rb', line 60 def singleton_method_added( meth ) d = %{ def #{meth}(*args) #{self.name}.#{meth}(self,*args) end } self.class_eval d #singleton_method_added_promoteself( meth ) if defined?(singleton_method_added_promoteself) super( meth ) #if defined?(singleton_method_added_promoteself) end |