Skip to content

Instantly share code, notes, and snippets.

@ka8725
Last active January 10, 2016 16:01
Show Gist options
  • Save ka8725/bd5af5d656d85ff30a2d to your computer and use it in GitHub Desktop.
Save ka8725/bd5af5d656d85ff30a2d to your computer and use it in GitHub Desktop.
class User
end
class UserChild < User
end
def cache
@cache ||= {}
end
def get_val(klass)
return 'x' if klass == Object
end
def pool(klass)
cache[klass.name] ||= begin
until val = get_val(klass)
klass = klass.superclass
break unless klass <= Object
end
cache[klass.name] = val
end
end
@cache = {}
pool(Object)
puts @cache.inspect # {"Object"=>"x"}
@cache = {}
pool(User)
puts @cache.inspect # {"Object"=>"x", "User"=>"x"}
@cache = {}
pool(UserChild)
puts @cache.inspect # {"Object"=>"x", "UserChild"=>"x"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment