From: Robert Klemme Date: 2010-07-13T01:25:06+09:00 Subject: Re: How to pass a hash as a param to a method called through eval? On 11.07.2010 23:00, 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. I wouldn't even use eval here - it's unsafe and slow. Something like this should work send(call['action'], *call['params']) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/