Skip to content

Instantly share code, notes, and snippets.

@rking

rking/foo.rb Secret

Created October 31, 2012 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rking/6326fc6c98a941bb879a to your computer and use it in GitHub Desktop.
Save rking/6326fc6c98a941bb879a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'active_support/core_ext/class/attribute'
class PropertyObject
class_attribute :fields
self.fields = []
def self.add_fields *f
self.fields += f
attr_accessor *f
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 ☝
@rking
Copy link
Author

rking commented Oct 31, 2012

(BTW, this is actually working, those comments at the bottom are out of date)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment