From: Thomas Gantner Date: 2007-08-26T05:40:45+09:00 Subject: Re: accessor_writer methods and instance_eval on Fri 24. August 2007 03.30, Nobuyoshi Nakada wrote: > Hi, > > At Fri, 24 Aug 2007 08:40:08 +0900, > Thomas Gantner wrote in [ruby-talk:266021]: >> Is there a way to avoid this behaviour without using 'self.a' or passing >> the struct as a parameter into the block (which quite obsoletes the >> meaning of instance_eval)? > > Nothing. It's one of reasons why using instance_eval for such > purpose is not a good idea. > Thanks for your quick answer (also to Ezra). I found a solution by defining setter-methods for every struct member: class C def initialize(&blk) @format = Struct.new( ... many, many elements ...) do members.each do |key| self.send(:define_method, "set_#{key}".to_sym) do |new_value| self[key] = new_value end end end @format.instance_eval(&blk) if block_given? end end Then I can use: c = C.new { set_elem8(8); set_elem17('test'); ... } Quite readable, but not many keystrokes saved. Is there a better alternative? I understand you suggest something without instance_eval()? -Thomas --