Below the sample modules (n numbers) which I am using in my project with the same method name(s) with different return value (prefix with module name).
module Example1
def self.ex_method
'example1_with_'
end
end
module Example2
def self.ex_method
'example2_with_'
end
end
I tried to accomplish this using metaprogramming way like #define_method. But, it's not working for me. Is there any way to do it?
array.each do |name|
Object.class_eval <<TES
module #{name}
def self.ex_method
"#{name.downcase}_with_"
end
end
TES
end
Error snap: You could see in the last line says that it's not completed.
m = Object.const_set("Example1", Module.new)
#=> Example1
m.define_singleton_method("ex_method") { 'example1_with' }
#=> :ex_method
Let's see:
Example1.is_a? Module
#=> true
Example1.methods.include?(:ex_method)
#=> true
Example1.ex_method
#=> "example1_with"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With