Skip to content

Instantly share code, notes, and snippets.

@externvoid
Last active March 8, 2018 10:31
Show Gist options
  • Select an option

  • Save externvoid/953c2ec16f12f7aa39060764d9f5ee32 to your computer and use it in GitHub Desktop.

Select an option

Save externvoid/953c2ec16f12f7aa39060764d9f5ee32 to your computer and use it in GitHub Desktop.
DSLの作り方
# クラスメソッド Enalbe_foo
module Product
def foo
puts "OK2"
end
end
class F
include Product
end
class Object
def acts_as_product
#def self.acts_as_product
include Product
end
end
class G
acts_as_product
end
class A < G
end
A.new.foo
#
module Foo
def foo
puts "OK foo"
end
end
module Enalbe_foo
module ClassMethods
def enable_foo
include Foo
end
end
def self.included(base)
base.extend(ClassMethods) # making class method
end
end
# この手法のメリットは?
Object.instance_eval{include Enalbe_foo}
class H
enable_foo # calling class method
end
class B < H
end
B.new.foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment