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
Post a Comment