Created
April 7, 2011 14:38
-
-
Save johnd/907875 to your computer and use it in GitHub Desktop.
#scotruby exercise
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 Mod | |
def my_accessor(field) | |
@my_attributes ||= Array.new | |
Mod.add_all_attributes(field) | |
attr_accessor field | |
@my_attributes << field | |
end | |
def my_attributes | |
@my_attributes | |
end | |
def self.all_attributes | |
@all_attributes | |
end | |
def self.add_all_attributes(field) | |
@all_attributes ||= Array.new | |
@all_attributes << field | |
end | |
end | |
class MyClass | |
self.extend(Mod) | |
my_accessor :field1 | |
my_accessor :field2 | |
end | |
class NewClass | |
self.extend(Mod) | |
my_accessor :field3 | |
end | |
m = MyClass.new | |
m.field1 = 99 | |
puts m.field1 | |
n = NewClass.new | |
n.field3 = 101 | |
puts n.field3 | |
p MyClass.my_attributes | |
p Mod.all_attributes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment