Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active April 25, 2016 05:10
Show Gist options
  • Save henrik/24147d0ee263063bef341797e993173e to your computer and use it in GitHub Desktop.
Save henrik/24147d0ee263063bef341797e993173e to your computer and use it in GitHub Desktop.
Sharing attributes in Ruby with a single `prepend` instead of e.g. `include` + `merge`. Proof of concept. Too implicit for my taste, I think.
module SharedAttributes
def attributes
super.merge(shared: true)
end
end
class FooAttributes
prepend SharedAttributes
def attributes
{ foo: true }
end
def speak
p attributes
end
end
class BarAttributes
prepend SharedAttributes
def attributes
{ bar: true }
end
def speak
p attributes
end
end
FooAttributes.new.speak # { foo: true, shared: true }
BarAttributes.new.speak # { bar: true, shared: true }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment