Created
September 6, 2012 10:05
-
-
Save Mekajiki/3654263 to your computer and use it in GitHub Desktop.
Ruby Module and Class
This file contains hidden or 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
module MyModule | |
@@module_v = 'module variable' | |
@instance_v = 'instance variable' | |
def set_m(value) | |
@@module_v = value | |
end | |
def print_m | |
p @@module_v | |
end | |
def set_i(value) | |
@instance_v = value | |
end | |
def print_i | |
p @instance_v | |
end | |
end | |
class MyClass | |
include MyModule | |
@instance_v = 'piyo' | |
def my_set_i(value) | |
@instance_v = value | |
end | |
end | |
i = MyClass.new | |
i.print_m | |
i.print_i | |
i.set_i 'hoge' | |
i.print_i | |
i.my_set_i 'hogefuga' | |
i.print_i | |
include MyModule | |
MyModule.print_i | |
print_i |
This file contains hidden or 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
$ ruby test.rb | |
"module variable" | |
nil | |
"hoge" | |
"hogefuga" | |
"instance variable" | |
nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment