Abstract classes in Ruby.
Like modules, abstract classes cannot be instantiated.
Unlike modules, abstract classes can be inherited and their derived classes can be instantiated.
gem install abstract_class
To make a class abstract, simply extend the
module ActiveRecord class Base extend AbstractClass end end
Any attempts to initialize or allocate an instance of an abstract class raises
ActiveRecord::Base.new #=> AbstractClass::Error - abstract class ActiveRecord::Base can't be instantiated ActiveRecord::Base.allocate #=> AbstractClass::Error - abstract class ActiveRecord::Base can't be allocated
Child classes can inherit from an abstract class.
class User < ActiveRecord::Base end
Instantiation and allocation behaves like normal for descendants of abstract classes.
User.new #=> #<User:0x003d066d5a861d> User.allocate #=> #<User:0x007f87588491d0>
bundle exec rspec
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
MIT - Copyright © 2011 Sean Huber