Simple class's variable accessor

Posted by root Sun, 20 Jul 2008 04:13:00 GMT

class Class
  def attr(arg)
    arg = arg.to_s
    if self.class_variables.include?(arg)
      class_eval %(def self.#{arg.sub(/^\@\@/, '')}; #{arg}; end)
    else
      raise ArgumentError, "No such class attribute", caller
    end
  end
end

class Test
  @@class_var = "class variable value"

  attr :@@class_var
end

puts Test.class_var => "class variable value"

Posted in  | Tags  | no comments

Comments

Comments are disabled