[suby-talk] meta and attr

TRANS transfire at gmail.com
Thu Sep 8 20:57:31 EDT 2005


For awhile I had worrying about what to name some convenience methods
for creating class attributes:

  def class_attr(*arg)
    (class << self; self; end).class_eval {
      attr(*args)
    }
  end

I thought class_attr was okay, but kind of long. I couldn't use cattr
b/c DHH already cooped it for creating class variable attributes. At
one point meta_attr came across my mind, when it hit me:

  class Object
    def meta
      class<<self; self; end
    end
  end

  module Module
    public :attr, :attr_reader, :attr_writer, :attr_accessor
  end

  class X
    meta.attr :n
  end

It works great, but there's that one caveat. Now the #attr_x methods
are all public. It doesn't really bother me at all, but it might
bother others.

Thoughts?

T.



More information about the suby-talk mailing list