Skip to content

Instantly share code, notes, and snippets.

@Mekajiki
Created September 6, 2012 10:05
Show Gist options
  • Save Mekajiki/3654263 to your computer and use it in GitHub Desktop.
Save Mekajiki/3654263 to your computer and use it in GitHub Desktop.
Ruby Module and Class
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
$ 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