memoize 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/CHANGES +7 -0
  2. data/README +11 -5
  3. data/lib/memoize.rb +13 -34
  4. data/test/tc_memoize.rb +1 -1
  5. metadata +24 -18
data/CHANGES CHANGED
@@ -1,3 +1,10 @@
1
+ = 1.2.1 - 2-Feb-2006
2
+ * Cache files are now read in binary mode to eliminate issues with Marshal
3
+ and MS Windows. Thanks go to Brian Buckley for the spot.
4
+ * Refactored code base down to about half previous size. Thanks go to Andrew
5
+ Johnson.
6
+ * Some documentation corrections.
7
+
1
8
  = 1.2.0 - 31-Dec-2005
2
9
  * Added support for memoizing to file rather than memory
3
10
  * Corresponding changes in the test suite and example programs
data/README CHANGED
@@ -44,10 +44,9 @@ Memoize#memoize(method, file=nil)
44
44
  you the value from the table instead of letting the method compute the
45
45
  value again.
46
46
 
47
- If +file+ is provided, the results are cached to that file instead of
48
- in memory. Note that this uses Marshal internally, so don't expect your
49
- cache to work between different versions of Ruby, should you happen to
50
- upgrade.
47
+ If +file+ is provided, the results are cached to that file. Note that
48
+ this uses Marshal internally. Beware of changes in the Marshal format
49
+ should you happen to upgrade.
51
50
 
52
51
  Returns the cache, which you can inspect or manipulate directly if you are
53
52
  so inclined.
@@ -55,13 +54,20 @@ Memoize#memoize(method, file=nil)
55
54
  = Acknowledgements
56
55
  Code borrowed from Nobu Nakada (ruby-talk:155159).
57
56
  Code borrowed from Ara Howard (ruby-talk:173428).
57
+ Code borrowed from Andrew Johnson (http://tinyurl.com/8ymx8)
58
58
  Ideas taken from Brian Buckley and Sean O'Halpin.
59
+ Tiny URL provided for Andrew Johnson because I could not find the ruby-talk
60
+ reference. The gateway may have been broken at the time.
61
+
62
+ == Known Issues
63
+ None that I'm aware of. Please report any problems on the Shards tracker
64
+ or the forums at http://www.rubyforge.org/projects/shards.
59
65
 
60
66
  == License
61
67
  Ruby's
62
68
 
63
69
  == Copyright
64
- (C) 2005, Daniel J. Berger
70
+ (C) 2005, 2006 Daniel J. Berger
65
71
  All Rights Reserved
66
72
 
67
73
  = Author
@@ -1,39 +1,18 @@
1
1
  module Memoize
2
- MEMOIZE_VERSION = "1.2.0"
2
+ MEMOIZE_VERSION = "1.2.1"
3
3
 
4
4
  # Memoize the method +name+. If +file+ is provided, then the method results
5
- # are stored on disk rather than in memory. This consumes virtually no
6
- # memory and is persistant.
5
+ # are stored on disk as well as in memory.
7
6
  def memoize(name, file=nil)
8
- meth = method(name)
9
-
10
- if file
11
- cache = Hash.new.update(Marshal.load(File.read(file))) rescue {}
12
- else
13
- cache = {}
14
- end
15
-
16
- if file
17
- (class << self; self; end).class_eval do
18
- define_method(name) do |*args|
19
- unless cache.has_key?(args)
20
- cache[args] = meth.call(*args)
21
- File.open(file, "wb+"){|f| Marshal.dump(cache, f) }
22
- end
23
- cache[args]
24
- end
25
- end
26
- else
27
- (class << self; self; end).class_eval do
28
- define_method(name) do |*args|
29
- if cache.has_key?(args)
30
- cache[args]
31
- else
32
- cache[args] ||= meth.call(*args)
33
- end
34
- end
35
- end
36
- end
37
- cache
7
+ cache = File.open(file, "rb"){ |io| Marshal.load(io) } rescue {}
8
+
9
+ (class<<self; self; end).send(:define_method, name) do |*args|
10
+ unless cache[args]
11
+ cache[args] = super
12
+ File.open(file, "wb+"){ |f| Marshal.dump(cache, f) } if file
13
+ end
14
+ cache[args]
15
+ end
16
+ cache
38
17
  end
39
- end
18
+ end
@@ -20,7 +20,7 @@ class TC_Memoize < Test::Unit::TestCase
20
20
  def setup
21
21
  @cache1 = nil
22
22
  @cache2 = nil
23
- @file = ENV["HOME"] || ENV["USERPROFILE"] + "/test.cache"
23
+ @file = (ENV["HOME"] || ENV["USERPROFILE"]) + "/test.cache"
24
24
  end
25
25
 
26
26
  def fib(n)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.10
2
+ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: memoize
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.2.0
7
- date: 2005-12-31
6
+ version: 1.2.1
7
+ date: 2006-02-02 00:00:00 -07:00
8
8
  summary: Speeds up methods at the cost of memory (or disk space)
9
9
  require_paths:
10
- - lib
10
+ - lib
11
11
  email: djberg96@gmail.com
12
12
  homepage: http://www.rubyforge.org/projects/shards
13
13
  rubyforge_project:
@@ -18,27 +18,33 @@ bindir: bin
18
18
  has_rdoc: true
19
19
  required_ruby_version: !ruby/object:Gem::Version::Requirement
20
20
  requirements:
21
- -
22
- - ">"
23
- - !ruby/object:Gem::Version
24
- version: 0.0.0
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
25
24
  version:
26
25
  platform: ruby
26
+ signing_key:
27
+ cert_chain:
27
28
  authors:
28
- - Daniel J. Berger
29
+ - Daniel J. Berger
29
30
  files:
30
- - lib/memoize.rb
31
- - README
32
- - CHANGES
33
- - MANIFEST
34
- - test/tc_memoize.rb
31
+ - lib/memoize.rb
32
+ - CHANGES
33
+ - MANIFEST
34
+ - README
35
+ - test/tc_memoize.rb
35
36
  test_files:
36
- - test/tc_memoize.rb
37
+ - test/tc_memoize.rb
37
38
  rdoc_options: []
39
+
38
40
  extra_rdoc_files:
39
- - README
40
- - CHANGES
41
+ - README
42
+ - CHANGES
41
43
  executables: []
44
+
42
45
  extensions: []
46
+
43
47
  requirements: []
44
- dependencies: []
48
+
49
+ dependencies: []
50
+