Created
January 16, 2012 23:10
-
-
Save eegrok/1623513 to your computer and use it in GitHub Desktop.
singleton class / instance_methods stuff
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 characters
| class Blee | |
| def self.classmeth | |
| puts 'calling classmeth' | |
| end | |
| def instmeth | |
| puts 'calling instmeth' | |
| end | |
| end | |
| Blee.singleton_class.class_eval do | |
| attr_accessor :class_level_attr_accessor | |
| def anotherclassmeth | |
| puts 'this is another class meth -- a more complicated way of doing the same as classmeth above' | |
| end | |
| end | |
| puts Blee.object_id | |
| puts Blee.singleton_class.object_id #different than object_id -- it's own class | |
| puts Blee.class #Class | |
| puts Blee.singleton_class.class #class | |
| puts Blee.singleton_class.instance_methods #has classmeth, anotherclassmeth, books_published, books_published=, but not instmeth | |
| puts Blee.singleton_class.instance_methods == Blee.methods #true | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment