From: walter@... Date: 2004-01-19T09:40:20+09:00 Subject: Re: setting instance variable "dynamically" > I saw in the pickaxe there's a way to call methods dynamically, with > send("method_name"): > > "test".send("length") # 4 > > What I want to do is set a variable this way. Is it possible? > Using the following class Q attr_accessor :field end q = Q.new here are some times for looping 1000000 times setting the index: i.e. 1.upto(1000000){|i| } q.send("field=", i) took 2.203 seconds q.send(:field=, i) took 1.251 seconds q.instance_eval{field = i} took 4.187 seconds q.instance_variable_set "@field", i took 2.032 seconds q.instance_variable_set :@field, i took 1.072 seconds q.field = i took 0.921 seconds P.S. These timings are for the PragProg installer ruby 1.8.0 (2003-08-04) [i386-mswin32] Walt ***************************************************** Walter Szewelanczyk IS Director M.W. Sewall & CO. email : walter@mwsewall.com 259 Front St. Phone : (207) 442-7994 x 128 Bath, ME 04530 Fax : (207) 443-6284 *****************************************************