[ANN] Defaultable 0.0.3

Discussion in 'Ruby' started by John W. Long, May 4, 2005.

  1. John W. Long

    John W. Long Guest

    This is the first public release of a very small project I've been
    working on recently. It's very similar actually to Ara.T.Howard's
    implementation of Traits.

    The goal of the project is to provide a mixin for objects that adds a
    series custom attribute methods for default values:

    require 'defaultable'

    class TestObject
    include Defaultable
    default_accessor :test => 'Hello world!'

    o = TestObject.new
    puts o.test #=> 'Hello world!'

    If the object's constructor has a hash as the last parameter it assumes
    it's an options hash and automatically extracts values for the object
    from it:

    class TestObject
    def initialize(options = {})

    o = TestObject.new:)test => 'Cool beans!')
    puts o.test #=> 'Cool beans!'

    The current implementation also works well with inheritance allowing
    inherited objects to inherit their parent object's attribute defaults.

    I'm particularly interested in feedback on the implementation. I'm not a
    metaclass guru, so for those of you who are, I would especially
    appreciate feedback on the class methods "included" and "method_added"
    on the module. These methods are currently hacks to make it useful, but
    their implementation isn't pretty.



    Defaultable is currently part of another small project which can be
    downloaded here:

    John W. Long, May 4, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.