Skip to content

Instantly share code, notes, and snippets.

View emmanuellyautomated's full-sized avatar

Emmanuel Obi emmanuellyautomated

  • Chicago, IL
View GitHub Profile
@emmanuellyautomated
emmanuellyautomated / search.rb
Last active April 6, 2016 22:24
Recursively traverses a hash and returns specified value if present along with keypath
def search(hash, val, stack=[], keyring=[])
stash=[]
keys = hash.keys
before = stack.length
until keys.length == 0
key = keys.pop
if hash[key].class == Hash
keyring << key
stack << hash[key]
@emmanuellyautomated
emmanuellyautomated / search_with_debug.rb
Last active April 7, 2016 03:46
search function that returns value and keypath --- has debug print statements
def search(hash, val, stack=[], keyring=[], stash=[])
keys = hash.keys
puts ">" * 50
puts "KEYS: #{keys}"
before = stack.length
puts "." * 20
puts "BEFORE: #{before}"
until keys.length == 0
@emmanuellyautomated
emmanuellyautomated / simple_search.rb
Created April 6, 2016 14:06
Retrieves value from hash if present; returns `false` otherwise
def search(house, val, houses=[])
keys = house.keys
until keys.count == 0
key = keys.pop
houses << house[key] if house[key].class == Hash
return house[key] if house[key] == val
end
return houses.empty? ? false : search(houses.pop, val, houses)
end
@emmanuellyautomated
emmanuellyautomated / deeply_nested_hashes
Last active April 6, 2016 14:05
Two deeply-nested hashes
'{
"foo_code": 404,
"foo_rbody": {
"query": {
"info": {
"acme_no": "444444",
"road_runner": "123"
},
"error": "no_lunch",
"message": "runner problem."