Created
November 5, 2010 21:28
-
-
Save keithrbennett/664901 to your computer and use it in GitHub Desktop.
class/self Example
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
| module C | |
| class << self | |
| def f | |
| end | |
| end | |
| end | |
| module D | |
| def self.f | |
| end | |
| end | |
| puts "f is a C class method?: #{C.methods.include?('f') ? 'Yes' : 'No' }" | |
| puts "f is a D class method?: #{D.methods.include?('f') ? 'Yes' : 'No' }" | |
| puts | |
| puts "C.singleton_methods = #{C.singleton_methods.join(', ')}" | |
| puts "D.singleton_methods = #{D.singleton_methods.join(', ')}" | |
| puts | |
| s = 'foo' | |
| class << s | |
| def twice | |
| self * 2 | |
| end | |
| end | |
| puts "Call twice: #{s.twice}" | |
| puts "s.singleton_methods = #{s.singleton_methods}" | |
| =begin | |
| Output is: | |
| f is a C class method?: Yes | |
| f is a D class method?: Yes | |
| C.singleton_methods = f | |
| D.singleton_methods = f | |
| Call twice: foofoo | |
| s.singleton_methods = twice | |
| =end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment