Module: SingletonMethodCreatorMixin

Included in:
ClassSettingsMixinClassMethods
Defined in:
lib/singleton_creator_mixin.rb

Instance Method Summary collapse

Instance Method Details

#create_singleton_method(method_name, lambda_obj) ⇒ Object

Create a method out of a name and a lambda.

Example:

my_lambda = lambda {13}
create_singleton_method :return_13, my_lambda

assert 13 == self.return_13


11
12
13
14
15
# File 'lib/singleton_creator_mixin.rb', line 11

def create_singleton_method method_name, lambda_obj
  sclass = class << self; self end
  sclass.send(:define_method, method_name, lambda_obj)
  sclass.send(:public, method_name)
end

#create_singleton_value_method(name, value) ⇒ Object

Create a method out of a value and a name The method will return the value.

Example:

create_singleton_value_method :return_14, 14

assert 14 == self.return_14


26
27
28
# File 'lib/singleton_creator_mixin.rb', line 26

def create_singleton_value_method name, value
  create_singleton_method(name, lambda { value })
end