From: "David A. Black" Date: 2010-07-12T06:07:37+09:00 Subject: Re: How to pass a hash as a param to a method called through eval? Hi -- On Mon, 12 Jul 2010, Brian Candler wrote: > Just use 'puts' instead of 'eval' to see what's happening. > >>> json = < { > "action": "someFunc", > "params": { > "a": "foo", > "b": "bar", > "c": "etc" > } > } > EOS > => "{\n \"action\": \"someFunc\",\n \"params\": {\n \"a\": > \"foo\",\n \"b\": \"bar\",\n \"c\": \"etc\"\n }\n}\n" >>> require 'rubygems' > => true >>> require 'json' > => true >>> call = JSON.parse(json) > => {"action"=>"someFunc", "params"=>{"a"=>"foo", "b"=>"bar", > "c"=>"etc"}} >>> puts "#{call['action']} #{call['params']}" > someFunc afoobbarcetc > => nil >>> puts "#{call['action']}('#{call['params']}')" > someFunc('afoobbarcetc') > => nil >>> > > Should be pretty obvious now, remember that eval is just interpreting > that string as a piece of ruby code. Footnote: in 1.9, Hash#to_s has changed such that (like Array#to_s) it returns more of an inspect string: >> puts "#{call['action']}(#{call['params']})" someFunc({"a"=>"foo", "b"=>"bar", "c"=>"etc"}) (I concur however in your point that send is almost certainly a better choice anyway.) David -- David A. Black, Senior Developer, Cyrus Innovation Inc. The Ruby training with Black/Brown/McAnally Compleat Stay tuned for next event announcement! Rubyist http://www.compleatrubyist.com