Last active
January 10, 2016 16:01
-
-
Save ka8725/bd5af5d656d85ff30a2d to your computer and use it in GitHub Desktop.
This file contains 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
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