Skip to content

Instantly share code, notes, and snippets.

@charlespeach
Created December 7, 2017 20:44
Show Gist options
  • Save charlespeach/c7680813ef6454f35b61669323a42d62 to your computer and use it in GitHub Desktop.
Save charlespeach/c7680813ef6454f35b61669323a42d62 to your computer and use it in GitHub Desktop.
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