The following code demonstare an example of passing a method deficed in the 'main' into a class via initializer.
class DemoClass
def initialize method
@method = method
end
def touch *args
puts 'touch'
@method.call *args
end
end
# Outside the gem
def some_method
puts 'Unbugged method'
end
def some_private_method
puts 'Bugged method'
end
non_bugged_instance = DemoClass.new( self.method :some_method )
bugged_instance = DemoClass.new( self.method :some_private_method )
puts "Non bugged touch"
non_bugged_instance.touch
puts "Bugged touch"
bugged_instance.touch
And output:
Non bugged touch
touch
Unbugged method
Bugged touch
touch
Bugged method
If you strongly wish to use only method names replace the class initializer with the following:
def initialize method_name
@method = Kernel.method method_name
end
And class creation call to as:
non_bugged_instance = DemoClass.new :some_method
bugged_instance = DemoClass.new :some_private_method
But I earnestly advise to use the first option.