-
-
Save rking/216e3fb8632b1d5c7aa7 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
class PropertyObject | |
def self.add_fields *f | |
@fields ||= [] | |
@fields += f | |
attr_accessor *f | |
class << self | |
define_method :fields do @fields end | |
end | |
end | |
def show | |
self.class.fields.each{|e| p send(e)} | |
end | |
end | |
class PhysicalObject < PropertyObject | |
add_fields :length, :height, :width | |
end | |
class HeavyObject < PhysicalObject | |
add_fields :weight | |
end | |
ho = HeavyObject.new | |
ho.length = 1 | |
ho.height = 2 | |
ho.width = 3 | |
ho.weight = 55 | |
p PhysicalObject.fields # => [:length, :height, :width] (which is correct) | |
p HeavyObject.fields # => [:weight] (want [:length, :height, :width, :weight]) | |
ho.show # => only "55", of course, because of ☝ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment