Last active
May 12, 2017 15:49
-
-
Save rklemme/7213313 to your computer and use it in GitHub Desktop.
Revisions
-
rklemme revised this gist
Oct 29, 2013 . 1 changed file with 13 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,26 +2,27 @@ module Foo module Foo4Class def reset_instance_count @instance_count = 0 end def new(*a, &b) super.tap { @instance_count += 1 } end def instance_count @instance_count end end def shout_instance_count printf "There have been %d of us created!\n", self.class.instance_count end # setup mechanics def self.included(cl) cl.extend(Foo4Class).reset_instance_count end end -
rklemme revised this gist
Oct 29, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,4 +29,4 @@ class Bar include Foo end Array.new(10) { Bar.new }.each(&:shout_instance_count) -
rklemme created this gist
Oct 29, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ #!/usr/bin/ruby -w module Foo module Foo4Class def instance_count @instance_count end end def self.included(cl) cl.extend Foo4Class cl.instance_variable_set '@instance_count', 0 cl.singleton_class.class_eval do def new(*a,&b) super.tap { @instance_count += 1 } end end end def shout_instance_count printf "There have been %d of us created!\n", self.class.instance_count end end class Bar include Foo end Array.new(10) { Bar.new }.first.shout_instance_count