Class-level readers and writers

Discussion in 'Ruby' started by Carl Youngblood, Nov 29, 2003.

  1. I've been working with the class attribute shortcuts that Hal introduced
    in THE RUBY WAY:

    class Class
    def cattr_reader(*syms)
    syms.each do |sym|
    class_eval <<-EOS
    if !defined? @@#{sym.id2name}
    @@#{sym.id2name} = nil
    end
    def self.~{sym.id2name}
    @@#{sym}
    end
    EOS
    end
    end

    def cattr_writer(*syms)
    syms.each do |sym|
    class_eval <<-EOS
    if !defined? @@#{sym.id2name}
    @@#{sym.id2name} = nil
    end
    def self.#{sym.id2name}=(obj)
    @@#{sym.id2name} = obj
    end
    EOS
    end
    end

    def cattr_accessor(*syms)
    cattr_reader(*syms)
    cattr_writer(*syms)
    end
    end

    I'm having a problem, however, in that classes that are inherited from
    the ones in which I declare these attrs don't seem to have them anymore.
    Does anyone know how I can make my declared class attributes get
    inherited?

    Thanks,
    Carl Youngblood
     
    Carl Youngblood, Nov 29, 2003
    #1
    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.