From: Ralph Shnelvar Date: 2009-11-24T00:44:08+09:00 Subject: Re: More on psuedo arrays; Better way? BC>> Marnen Laibow-Koser wrote: >>>> Is there a better way to do this? >>> >>> x.send("x#{rand(i % N_X_METHODS)]".to_sym)) BC>> And you don't need to_sym either. RS> This x.send() will be executed a million times. RS> Doesn't that have a fairly high overhead? Won't a million symbols be RS> generated? In my case ... with the m array, only 20 symbols would be RS> generated. RS> Does it matter? RS> Is it possible to run out of symbols? Argh ... I was the OP. That 10.times should have read 1_000_000.times (or some very large number of times). To refresh ... the following should have been the code ... N_X_METHODS = 20 class X 20.times do |i| define_method "x%02d=" % i do |arg| # n.b. x00= ... x19= already exist and I have no control over # there names or the fact that they were created that way. # the puts, below, is just a placeholder for a more complicated method puts "Hi " + i.to_s + arg.to_s end end end m = Array.new(N_X_METHODS) do |i| ("x%02d=" % i).to_sym end p m 1_000_000.times do |i| x = X.new # select a random method x.method(m[rand(i % N_X_METHODS)]).call(i) end