Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.