From: "Thomas R.Corbin" Date: 2000-12-25T06:10:02+09:00 Subject: [ruby-talk:8053] Re: Question about "attr_reader" on class level variables (thanks) GOTO Kentaro wrote: > In message "[ruby-talk:8024] Re: Question about "attr_reader" on class > level variables" > on 00/12/24, Dave Thomas writes: > >> 1. It seems like I've got to write an access method for class > >> variables and > >> cannot use attr_reader to get one automatically created for me. Is > >> that correct? > > > >Yes, but you can write your own version or attr_reader for class > >variables if you want. > > Yes! This is a Ruby's charm: > > class Class > def cattr_reader(*cvs) > cvs.each do |cv| > class_eval %Q[ > def self.#{cv}; @@#{cv} end > ] > end > end > end > > class Foo > @@foo = "Root" > @@bar = 66 > > cattr_reader :foo, :bar > end > > p [Foo.foo, Foo.bar] #=> ["Root", 66] > Wow, both these answers are sooo slick. I'm very impressed. Thank you both very much! Tom Corbin