Skip to content

Instantly share code, notes, and snippets.

@cagmz

cagmz/hash.rb Secret

Last active August 30, 2018 02:37
Show Gist options
  • Save cagmz/a9c3e752d14fc558e477fdd9525b700f to your computer and use it in GitHub Desktop.
Save cagmz/a9c3e752d14fc558e477fdd9525b700f to your computer and use it in GitHub Desktop.
# 50 MB + more memory
def contacts_for_vendor(vendor)
@_contacts_for_vendor ||=
Hash.new do |vendor_hash, v|
vendor_hash[v] =
contacts_for(v).select do |c|
v.ids.include?(c.login_id)
end
end
@_contacts_for_vendor[vendor]
end
# less memory
def contacts_hash
@_hash ||= {}
end
def contacts_for_vendor(vendor)
contacts_hash[vendor.id] ||= contacts_for(vendor).select do |c|
vendor.ids.include?(c.login_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment