Created
July 27, 2010 16:56
-
-
Save npverni/492501 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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