From: Ammar Ali Date: 2010-07-12T05:51:24+09:00 Subject: Re: How to pass a hash as a param to a method called through eval? --00504501724f75bb26048b22ccf5 Content-Type: text/plain; charset=UTF-8 On Sun, Jul 11, 2010 at 11:29 PM, Alex Stahl wrote: > Thanks, but... tried that already and it fails without even calling the > method: > > "...undefined method `com' for nil:NilClass (NoMethodError)" > > So I tried that w/ single quotes too: > > eval("#{call['action']}('#{call['params']}')") > > and that works, but still passes the params as a string, not a hash. :( > The argument should be a string, that's what eval expects. The problem with the first version (without the parentheses) was syntax. I don't know where the "com" or the nil:NilClass are coming from. Is there something missing from your code sample? Here's what I get in irb: mini:~ ammar$ rvm use 1.9.1 info: Using ruby 1.9.1 p378 mini:~ ammar$ irb ruby-1.9.1-p378 > require 'json' => true ruby-1.9.1-p378 > def some_func(hash); puts "from function: #{hash.inspect}"; end => nil ruby-1.9.1-p378 > j = '{ "action": "some_func", "params": { "a": "foo", "b": "bar" } }' => "{ \"action\": \"some_func\", \"params\": { \"a\": \"foo\", \"b\": \"bar\" } }" ruby-1.9.1-p378 > call = JSON.parse(j) => {"action"=>"some_func", "params"=>{"a"=>"foo", "b"=>"bar"}} ruby-1.9.1-p378 > eval("#{call['action']}(#{call['params']})") from function: {"a"=>"foo", "b"=>"bar"} => nil Ammar --00504501724f75bb26048b22ccf5--