Created
April 9, 2012 17:11
-
-
Save mkoby/2344793 to your computer and use it in GitHub Desktop.
Intro to Ruby - 11 - Modules
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
# 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