Skip to content

Instantly share code, notes, and snippets.

@jschairb
Last active December 19, 2015 15:49
Show Gist options
  • Save jschairb/5978739 to your computer and use it in GitHub Desktop.
Save jschairb/5978739 to your computer and use it in GitHub Desktop.
class AttributeAccessor < Module
def initialize(*names)
@_names = names
end
def included(model)
super
@_names.each { |n| attr_accessor n }
end
end
class Book
ATTRIBUTES = [:author, :title]
include AttributeAccessor.new(*ATTRIBUTES)
def title
"#{super} + super works!"
end
end
class Book
include AttributeAccessor.new(:isbn)
def isbn
"#{super} + whoops"
end
end
book = Book.new
book.title = "I'm not sure of the point"
book.author = "jschairb"
puts book.title
puts book.author
puts book.isbn
puts book.class.ancestors
I'm not sure of the point + super works!
jschairb
+ whoops
Book
#<AttributeAccessor:0x007fa3f9883930>
#<AttributeAccessor:0x007fa3f9883b10>
Object
Kernel
BasicObject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment