Created
December 7, 2017 20:44
-
-
Save charlespeach/c7680813ef6454f35b61669323a42d62 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
def find_in(collection:, query:) | |
result = collection.dig(*query[:object_path]) | |
case result | |
when Array then | |
return result[query[:position]] | |
else | |
return result | |
end | |
end | |
# Example data | |
data = { | |
foo: { | |
bar: { | |
baz: ['can you', 'find', 'me?', '!found!'] | |
} | |
} | |
} | |
# query built from imaginary search that has already | |
# happened | |
query = { | |
object_path: [:foo, :bar, :baz], | |
position: 3 | |
} | |
puts find_in(collection: data, query: query) | |
# => !found! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment