From: Tim Conner Date: 2009-04-27T02:40:05+09:00 Subject: Using method missing to create getters and setters Hi, I am trying to create a model (BatchExternalBooking) which has the following methods: BatchExternalBooking.message_thread_1= BatchExternalBooking.message_thread_1 BatchExternalBooking.message_thread_2= BatchExternalBooking.message_thread_2 ... etc all the way upto BatchExternalBooking.message_thread_x= BatchExternalBooking.message_thread_x I assume that I need to do this via method_missing because I don't know how many of these methods I will actually need. My code currently looks like this: class BatchExternalBooking def method_missing(method_sym, *args) if method_sym.to_s =~ /^message_thread_([0-9]*)=?(\w*)?$/ BatchExternalBooking.instance_eval "attr_accessor :message_thread_#{$1}" self.send("message_thread_#{$1}=", $2) else super end end end However, this is not working. See below: >> b = BatchExternalBooking.new => # >> b.message_thread_1 = 45 => 45 >> b.message_thread_1 => "" The value wasn't actually set but the attr_accessor correctly created the setter method. If I try and set the variable again, it works: >> b.message_thread_1 = 45 => 45 >> b.message_thread_1 => 45 Why isn't the setter working the first time round? Thanks a lot. -- Posted via http://www.ruby-forum.com/.