Skip to content

Instantly share code, notes, and snippets.

@johnd
Created April 7, 2011 14:38
Show Gist options
  • Save johnd/907875 to your computer and use it in GitHub Desktop.
Save johnd/907875 to your computer and use it in GitHub Desktop.
#scotruby exercise
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