Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 11th, 2012  |  syntax: None  |  size: 1.05 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/opt/local/bin/ruby
  2. class Foo
  3.         attr_accessor :cache
  4.         def cache
  5.                 @cache ||= {}
  6.         end
  7.  
  8.         def flush               # looks correct
  9.                 return 0 if cache.length == 0
  10.                 puts "Flushing #{cache.size}/#{@cache.size} rows."
  11.                 @cache = {}
  12.                 puts "Flushed; #{cache.size}/#{@cache.size} rows."
  13.         end
  14.  
  15.         def flush2              # returns different post-flush sizes
  16.                 return 0 if cache.length == 0
  17.                 puts "Flushing #{cache.size}/#{@cache.size} rows."
  18.                 cache = {}
  19.                 puts "Flushed; #{cache.size}/#{@cache.size} rows."
  20.         end
  21.  
  22.         def flush3              # looks correct
  23.                 return 0 if cache.length == 0
  24.                 puts "Flushing #{cache.size}/#{@cache.size} rows."
  25.                 self.cache = {}
  26.                 puts "Flushed; #{cache.size}/#{@cache.size} rows."
  27.         end
  28.  
  29.         def fill
  30.                 cache["a"] = :a
  31.                 cache["b"] = :b
  32.                 cache["c"] = :c
  33.                 cache["d"] = :d
  34.                 cache["e"] = :e
  35.         end
  36. end
  37.  
  38. if $0 == __FILE__
  39.         def tryItOut
  40.                 foo = Foo.new
  41.                 foo.flush       # returns silently: no data
  42.                 foo.fill
  43.                 foo.flush       # looks correct
  44.                 foo.fill
  45.                 foo.flush2      # shows different post-flush sizes
  46.                 foo.fill
  47.                 foo.flush3      # looks correct
  48.         end
  49.         tryItOut
  50. end