Skip to content

Instantly share code, notes, and snippets.

@mkoby
Created April 9, 2012 17:11
Show Gist options
  • Save mkoby/2344793 to your computer and use it in GitHub Desktop.
Save mkoby/2344793 to your computer and use it in GitHub Desktop.
Intro to Ruby - 11 - Modules
# Basic Module
module MyModule
def module_hello
return “MyModule module_hello method”
end
end
# Class with module included
class ClassOne
include MyModule
end
# Another class with module included
class ClassTwo
include MyModule
end
# Create an object from ClassOne class
c1 = ClassOne.new
# List all methods that have "hello" in their name
c1.methods.grep /hello/ # => [... "module_hello", ...]
# Run module_hello method on c1 object
c1.module_hello # => "MyModule module_hello_method"
# Create object from ClassTow class
c2 = ClassTwo.new
# Run module_hello method on c2 object
c2.module_hello # => "MyModule module_hello_method"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment