From: "David A. Black" Date: 2009-07-02T06:11:58+09:00 Subject: Re: attr_writers within blocks Hi -- On Thu, 2 Jul 2009, Wijnand Wiersma wrote: > > Op 30 jun 2009, om 09:29 heeft Fabian Streitel het volgende geschreven: > >> I can only recommend this thread: http://www.ruby-forum.com/topic/190337 >> We had a nice discussion there on those kind of DSLish attr setters and >> what would be the best way to implement them > > I actually like one of the proposed ways in that topic. > Using a method that is both getter and setter. > I decided to create a attr_accessor_special like this: > def attr_accessor_special(*syms) > syms.each do | sym | > name=sym.to_s > send(:define_method, name) do | *args | > value = args[0] > self.instance_variable_set("@#{name}", value) if value > self.instance_variable_get("@#{name}") > end > end > end > > No way to make a value nil again but I don't think that will be needed in my > case. You could test to see whether args is empty (see my earlier post). David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Now available: The Well-Grounded Rubyist (http://manning.com/black2) "Ruby 1.9: What You Need To Know" Envycasts with David A. Black http://www.envycasts.com