ruby - Is there a way to a regex query on a json structure's keys? -


say have have list of json structure such

{ "s1" => "foo", "r2" => "bar", "s2" => "baz" }

and want data "s*" keys,

how in ruby? there way perform such task?

thanks,

use select pick out key/value pairs want:

{ "s1" => "foo", "r2" => "bar", "s2" => "baz" }.select{|k,v| k =~ /^s/} 

the result desired hash - if using ruby 1.9/2.0. however, in ruby 1.8 return array of arrays - can wrap hash[] turn hash:

start = { "s1" => "foo", "r2" => "bar", "s2" => "baz" } hash[start.select{|k,v| k =~ /^s/}] 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -