
Untitled
By: a guest on
Jul 11th, 2012 | syntax:
None | size: 1.05 KB | hits: 16 | expires: Never
#!/opt/local/bin/ruby
class Foo
attr_accessor :cache
def cache
@cache ||= {}
end
def flush # looks correct
return 0 if cache.length == 0
puts "Flushing #{cache.size}/#{@cache.size} rows."
@cache = {}
puts "Flushed; #{cache.size}/#{@cache.size} rows."
end
def flush2 # returns different post-flush sizes
return 0 if cache.length == 0
puts "Flushing #{cache.size}/#{@cache.size} rows."
cache = {}
puts "Flushed; #{cache.size}/#{@cache.size} rows."
end
def flush3 # looks correct
return 0 if cache.length == 0
puts "Flushing #{cache.size}/#{@cache.size} rows."
self.cache = {}
puts "Flushed; #{cache.size}/#{@cache.size} rows."
end
def fill
cache["a"] = :a
cache["b"] = :b
cache["c"] = :c
cache["d"] = :d
cache["e"] = :e
end
end
if $0 == __FILE__
def tryItOut
foo = Foo.new
foo.flush # returns silently: no data
foo.fill
foo.flush # looks correct
foo.fill
foo.flush2 # shows different post-flush sizes
foo.fill
foo.flush3 # looks correct
end
tryItOut
end