Skip to content

Instantly share code, notes, and snippets.

@manoj2411
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj2411/4a6a781acb358fdef08c to your computer and use it in GitHub Desktop.
Save manoj2411/4a6a781acb358fdef08c to your computer and use it in GitHub Desktop.
A module that shares code with personalised method names with respect to class
module ModuleWithPersonalisedMethods
def self.included(klass)
@methods_definer.call(klass)
end
@methods_definer = lambda do |klass|
klass_name = klass.name.downcase
klass.class_exec do
define_method "#{klass_name}_m1" do |arg1 = nil|
arg1 || "#{klass}_m1"
end
define_method "#{klass_name}_m2" do |arg1 = nil|
arg1 || "#{klass}_m2"
end
define_method "#{klass_name}_m3" do |arg1 = nil|
arg1 || "#{klass}_m3"
end
end
end
end
class A
include ModuleWithPersonalisedMethods
end
class B
include ModuleWithPersonalisedMethods
end
p A.new.methods.select{|e| e.to_s.start_with? "a_"}
# => [:a_m1, :a_m2, :a_m3]
p B.new.methods.select{|e| e.to_s.start_with? "b_"}
# => [:b_m1, :b_m2, :b_m3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment