Skip to content

Instantly share code, notes, and snippets.

@npverni
Created July 27, 2010 16:56
Show Gist options
  • Select an option

  • Save npverni/492501 to your computer and use it in GitHub Desktop.

Select an option

Save npverni/492501 to your computer and use it in GitHub Desktop.
require "rubygems"
require "json"
data = '[
{
"name": "John Doe",
"location": {
"name": "New York, New York",
"id": 12746342329
},
"hometown": {
"name": "Brooklyn, New York",
"id": 43453644
}
},
{
"name": "Jane Doe",
"location": {
"name": "Miami, Florida",
"id": 12746342329
},
"hometown": {
"name": "Queens, New York",
"id": 12746329
}
},
{
"name": "Foo Doe",
"location": {
"name": "Brooklyn, NY",
"id": 12746342329
},
"hometown": {
"name": "Orlando, Florida",
"id": 12746329
}
},
{
"name": "Foo Baz",
"location": {
"name": "Brooklyn, California",
"id": 12746342329
},
"hometown": {
"name": "Orlando, California",
"id": 12746329
}
}
]'
friends = JSON.parse(data)
def friends_in_states(friends, states=[])
friends.select do |f|
states.map{|s| (f['location']['name'] =~ Regexp.new(s)) || (f['hometown']['name'] =~ Regexp.new(s)) }.any?
end
end
in_state_friends = friends_in_states(friends, ['Florida', 'New York'])
puts in_state_friends
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment