Skip to content

Instantly share code, notes, and snippets.

@mdesjardins
Created October 29, 2011 13:56
Show Gist options
  • Save mdesjardins/1324482 to your computer and use it in GitHub Desktop.
Save mdesjardins/1324482 to your computer and use it in GitHub Desktop.
Same assignment operator thing with more print statements
class FakeCache
def self.write(key,value)
File.open(key,'w') { |f| f.write(value) }
end
def self.read(key)
begin
File.open(key).each_line{ |s| result = s }
result
rescue
return nil
end
end
end
class TestOp
def self.cache_key=(num)
#Rails.cache.write('restaurant_cache_key', num)
puts "CALLING THE ASSIGNMENT OPERATOR." #### <====== ADDED THIS!!!!
FakeCache.write('restaurant_cache_key', num)
end
def self.cache_key
#unless r = Rails.cache.read('restaurant_cache_key')
unless r = FakeCache.read('restaurant_cache_key')
puts "IN THE RAND BLOCK." #### <====== ADDED THIS!!!!
r = rand(10000)
FakeCache.write('restaurant_cache_key', r.to_s)
end
return r.to_i
end
end
# start w/ a clean slate
File.delete('restaurant_cache_key')
puts TestOp.cache_key
TestOp.cache_key += 1
puts TestOp.cache_key
# ~/_work/elctech/temp> ruby testop.rb
# IN THE RAND BLOCK.
# 3988
# IN THE RAND BLOCK.
# CALLING THE ASSIGNMENT OPERATOR.
# IN THE RAND BLOCK.
# 8156
# ~/_work/elctech/temp> ruby testop.rb
# IN THE RAND BLOCK.
# 5821
# IN THE RAND BLOCK.
# CALLING THE ASSIGNMENT OPERATOR.
# IN THE RAND BLOCK.
# 4281
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment