Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Last active August 29, 2015 14:06
Show Gist options
  • Save Phrogz/c0e33e9b7c8cc23f56aa to your computer and use it in GitHub Desktop.
Save Phrogz/c0e33e9b7c8cc23f56aa to your computer and use it in GitHub Desktop.
Merging class attributes up the tree
class Ancestor
@props = {a:1, b:1, c:1}
def self.merged
(ancestors[1].respond_to?(:merged) ? ancestors[1].merged : {}).merge(@props)
end
end
class Child < Ancestor
@props = {a:2}
end
class GChild < Child
@props = {c:3}
end
p GChild.merged
#=> {:a=>2, :b=>1, :c=>3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment