From: dblack@... Date: 2007-02-17T00:32:17+09:00 Subject: Re: class design issues Hi -- On Fri, 16 Feb 2007, sur max wrote: > But i think there is a point to discuss here... > > as lf1.name is simply an instance method which is(should be) only capable of > returning the instance variable "@name" > > so by defining :name as attr_reader should block everything which is trying > to write the instance variable "@name" attr_reader just writes a wrapper method for you. If the default wrapper method doesn't do what you want, you can write a different one: def x @x.dup end If you want to do that a lot, you could write a new attr_* method: class Module def attr_reader_dup(*attrs) attrs.each do |attr| define_method(attr) { instance_variable_get("@#{attr}").dup } end end end class C attr_reader_dup(:x) def initialize @x = "don't change me" end end c = C.new c.x << "trying to change" p c.x # don't change me David P.S. Please don't top-post. Just quote what you're responding to and add your response. -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)