Skip to content

Instantly share code, notes, and snippets.

@hisaichi5518
Created April 19, 2014 15:19
Show Gist options
  • Select an option

  • Save hisaichi5518/11087390 to your computer and use it in GitHub Desktop.

Select an option

Save hisaichi5518/11087390 to your computer and use it in GitHub Desktop.
includeしただけで、クラスメソッドも生やしたい
$ ruby t.rb
{:push=>[#<Proc:[email protected]:27>]}
{:push=>[#<Proc:[email protected]:27>, #<Proc:[email protected]:29>]}
{:push=>[#<Proc:[email protected]:32>]}
{:push=>[#<Proc:[email protected]:32>, #<Proc:[email protected]:34>]}
{:push=>[#<Proc:[email protected]:37>]}
{:push=>[#<Proc:[email protected]:37>, #<Proc:[email protected]:39>]}
module Tiger
module ClassMethods
def on(name, &code)
@triggers ||= {}
@triggers[name] ||= []
@triggers[name].push(code)
p @triggers
end
end
include ClassMethods
def self.included(klass)
klass.extend ClassMethods
end
end
class TestTigerClass
include Tiger
end
module TestTigerModule
include Tiger
end
test_tiger_class = TestTigerClass.new
test_tiger_class.on(:push) do
end
test_tiger_class.on(:push) do
end
TestTigerClass.on(:push) do
end
TestTigerClass.on(:push) do
end
TestTigerModule.on(:push) do
end
TestTigerModule.on(:push) do
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment