Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Created May 31, 2015 01:37
Show Gist options
  • Save cupakromer/5800e3cafd3c1b8beaab to your computer and use it in GitHub Desktop.
Save cupakromer/5800e3cafd3c1b8beaab to your computer and use it in GitHub Desktop.
Module Functions
module ExampleModuleFunctions
def self.use_class_method
"module singleton class method"
end
def use_normal_method
"normally included method"
end
module_function
def use_module_function
"this is a module function"
end
end
ExampleModuleFunctions.use_class_method
# => "module singleton class method"
ExampleModuleFunctions.use_module_function
# => "this is a module function"
obj = Object.new
obj.extend ExampleModuleFunctions
obj.use_normal_method
# => "normally included method"
obj.use_class_method
# => NoMethodError: undefined method `use_class_method'
obj.use_module_function
# => NoMethodError: private method `use_module_function' called
class Foo
include ExampleModuleFunctions
def delegate_to_module_function
use_module_function
end
end
foo = Foo.new
foo.use_normal_method
# => "normally included method"
foo.delegate_to_module_function
# => "this is a module function"
foo.use_module_function
# => NoMethodError: private method `use_module_function' called
foo.use_class_method
# => NoMethodError: undefined method `use_class_method'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment