public
Created

#scotruby exercise with extend

  • Download Gist
extend.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
module MyAttributes
@@attributes = []
 
def my_accessor(field)
@@attributes << field
attr_accessor(field)
end
 
def my_attributes
@@attributes
end
end
 
class MyClass
extend MyAttributes
 
my_accessor :field1
my_accessor :field2
end
 
class SomeOtherClass
extend MyAttributes
 
my_accessor :field3
my_accessor :field4
end
 
m = MyClass.new
m.field1 = 99
 
n = SomeOtherClass
puts m
puts m.field1
p MyClass.my_attributes
p SomeOtherClass.my_attributes

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.