From: Rick DeNatale Date: 2009-04-07T06:12:23+09:00 Subject: Re: Method missing and arrays as arguments.. --00163628360a1a9c720466e95c07 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Mon, Apr 6, 2009 at 4:35 PM, Kyle Schmitt wrote: > How do you go about passing arrays as arguments to something like > method_missing? I've got a method that takes an array as an argument, > but calling it via method_missing is proving problematic. How do you > deal with arrays in these situations? > > def table_row(values) > # values is the list of items in the table row > #.... > end > > def method_missing(method,*args) > if respond_to?method.to_s[/^add_([^$]+)/,1] > @data< else > super method_missing(method,*args) > end > end I'm not sure exactly what you are trying to do, I'm guessing that if you send add_table_row(some_array) to an instance, the method_missing will call table_row and add the result to @data. The problem isn't the array argument, it's what you are doing in the true leg of the if. The first argument to method_missing is not a method, but a symbol. You are converting this symbol to a string, stripping off add_ from the beginning and they trying to call the string, which ain't gonna work. Instead you want to use send: class SomeClass attr_reader :data def table_row(values) # values is the list of items in the table row #.... values end def method_missing(symbol,*args) without_add = symbol.to_s[/^add_([^$]+)/,1] if respond_to? without_add @data ||= [] @data<< send(without_add, *args) else super method_missing(method,*args) end end end sc = SomeClass.new sc.add_table_row([1,2,3]) sc.data # => [[1, 2, 3]] I used lazy initialization to initialize @data, assuming that this is vaguely what you are after. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale --00163628360a1a9c720466e95c07--