Skip to content

Instantly share code, notes, and snippets.

@kstratis
Last active February 4, 2018 15:31
Show Gist options
  • Save kstratis/4610cb852bb5e42c4cb5c9c0e2c12724 to your computer and use it in GitHub Desktop.
Save kstratis/4610cb852bb5e42c4cb5c9c0e2c12724 to your computer and use it in GitHub Desktop.
Module methods
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