Last active
February 4, 2018 15:31
-
-
Save kstratis/4610cb852bb5e42c4cb5c9c0e2c12724 to your computer and use it in GitHub Desktop.
Module methods
This file contains 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 Module | |
# `hello` should be readily available within all regular classes | |
def hello | |
puts 'Hello World' | |
end | |
end | |
# `special` should be available within all regular singleton classes | |
class Module | |
class << self | |
def special | |
puts 'This is the special method defined in Module.singleton_class' | |
end | |
end | |
end | |
class MyClass | |
hello # => 'Hello World' | |
# special # this correctly raises an error here | |
# special should only available in MyClass.singleton_class and it actually is. | |
# However `hello` method shouldn't be available in `MyClass.singleton_class` (only in MyClass) but yet it is. Why? | |
class << self | |
special # This only works here as it should | |
hello # but strangely this works too! It shouldn't though... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment