From: Logan Capaldo Date: 2006-02-01T12:50:33+09:00 Subject: Re: memoize to a file On Jan 31, 2006, at 10:32 PM, Brian Buckley wrote: > Hello all, > > Using Memoize gem 1.2.0, memoizing TO a file appears to be working > for me > but subsequently reading that file (say, by rerunning the same script) > appears NOT to be working (the fib(n) calls are being run again). > Inspecting the Memoize module I changed the line > > cache = Hash.new.update(Marshal.load(File.read(file))) rescue { } > to > cache = Hash.new.update(Marshal.load(File.read(file))) > > and it instead of silently failing I now see the error message: "in > `load': > marshal data too short (ArgumentError)" > > My questions: > 1 What is causing this error? (possibly Windows related?) > 2 What is the purpose of the rescue{} suppressing the error info > in the > first place? > 3 Instead of using Marshall would using yaml be a reasonable > alternative? > (I am thinking of readability of the cache file and also capability to > pre-populate it) > > Thanks. > > -- Brian Buckley > > ------------------------------------------ > require 'memoize' > include Memoize > def fib(n) > puts "running... n is #{n}" > return n if n < 2 > fib(n-1) + fib(n-2) > end > h = memoize(:fib,"fib.cache") > puts fib(10) Basically it's using exceptions as flow control: begin cache = Hash.new.update(Marshal.load(File.read(file))) rescue cache = {} # empty hash end So for whatever reason, if loading the file fails (eg, this is the first time the program has been run) it just starts with an empty cache. I don't know why its failing to read the file.