memoize 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/CHANGES +6 -1
  2. data/MANIFEST +1 -0
  3. data/README +8 -4
  4. data/lib/memoize.rb +4 -3
  5. data/test/tc_memoize.rb +28 -1
  6. metadata +4 -6
data/CHANGES CHANGED
@@ -1,2 +1,7 @@
1
- = 1.0.0
1
+ = 1.1.0 - 20-Oct-2005
2
+ * Memoize#memoize now returns the cache (ruby-talk: 160870 and following)
3
+ * Memoize#memoize now caches nil and false results
4
+ * Corresponding doc and test changes
5
+
6
+ = 1.0.0 - 9-Sep-2005
2
7
  * Initial release
data/MANIFEST CHANGED
@@ -5,6 +5,7 @@ install.rb
5
5
  memoize.gemspec
6
6
 
7
7
  examples/fibonacci.rb
8
+ examples/test_memoize.rb
8
9
 
9
10
  lib/memoize.rb
10
11
 
data/README CHANGED
@@ -12,7 +12,7 @@
12
12
  == Gems Installation
13
13
  ruby test/tc_memoize.rb (optional)
14
14
  ruby memoize.gemspec
15
- gem install memoize-xyz.gem # Where 'xyz' is the version
15
+ gem install memoize-x.y.z.gem # Where 'x.y.z' is the version
16
16
 
17
17
  = Synopsis
18
18
  require "memoize"
@@ -34,13 +34,17 @@ Memoize::MEMOIZE_VERSION
34
34
  Returns the version of this package as a String.
35
35
 
36
36
  = Methods
37
- Memoize.memoize(method)
37
+ Memoize#memoize(method)
38
38
  Takes a method (symbol) and caches the results of +method+ in a hash table.
39
39
  If you call +method+ again with the same arguments, memoize gives you the
40
40
  value from the table instead of letting the method compute the value again.
41
41
 
42
+ Returns the cache, which you can inspect or manipulate directly if you are
43
+ so inclined.
44
+
42
45
  = Acknowledgements
43
- Code borrowd from Nobu Nakada (ruby-talk:155159).
46
+ Code borrowed from Nobu Nakada (ruby-talk:155159).
47
+ Ideas taken from Brian Buckley and Sean O'Halpin.
44
48
 
45
49
  == License
46
50
  Ruby's
@@ -51,5 +55,5 @@ Memoize.memoize(method)
51
55
 
52
56
  = Author
53
57
  Daniel J. Berger
54
- djberg96@gmail.com
58
+ djberg96 at gmail dot com
55
59
  IRC nick: imperator (freenode)
data/lib/memoize.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  module Memoize
2
- MEMOIZE_VERSION = "1.0.0"
2
+ MEMOIZE_VERSION = "1.1.0"
3
3
  def memoize(name)
4
4
  meth = method(name)
5
5
  cache = {}
6
6
  (class << self; self; end).class_eval do
7
7
  define_method(name) do |*args|
8
- cache[args] ||= meth.call(*args)
8
+ cache.has_key?(args) ? cache[args] : cache[args] ||= meth.call(*args)
9
9
  end
10
10
  end
11
- end
11
+ cache
12
+ end
12
13
  end
data/test/tc_memoize.rb CHANGED
@@ -17,18 +17,45 @@ require "memoize"
17
17
  class TC_Memoize < Test::Unit::TestCase
18
18
  include Memoize
19
19
 
20
+ def setup
21
+ @cache1 = nil
22
+ @cache2 = nil
23
+ end
24
+
20
25
  def fib(n)
21
26
  return n if n < 2
22
27
  fib(n-1) + fib(n-2)
23
28
  end
24
29
 
30
+ def factorial(n)
31
+ f = 1
32
+ n.downto(2) { |x| f *= x }
33
+ f
34
+ end
35
+
25
36
  def test_version
26
- assert_equal("1.0.0", Memoize::MEMOIZE_VERSION)
37
+ assert_equal("1.1.0", Memoize::MEMOIZE_VERSION)
27
38
  end
28
39
 
29
40
  def test_memoize
30
41
  assert_respond_to(self, :memoize)
42
+ assert_nothing_raised{ fib(5) }
31
43
  assert_nothing_raised{ memoize(:fib) }
32
44
  assert_nothing_raised{ fib(50) }
33
45
  end
46
+
47
+ # Ensure that a cache is returned, that it's a hash, and that each
48
+ # memoized method retains its own cache properly.
49
+ def test_memoize_cache
50
+ assert_nothing_raised{ @cache1 = self.memoize(:fib) }
51
+ assert_nothing_raised{ @cache2 = self.memoize(:factorial) }
52
+
53
+ assert_nothing_raised{ self.fib(3) }
54
+ assert_nothing_raised{ self.factorial(3) }
55
+
56
+ assert_kind_of(Hash, @cache1)
57
+ assert_kind_of(Hash, @cache2)
58
+
59
+ assert_not_equal(@cache1, @cache2)
60
+ end
34
61
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: memoize
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.0
7
- date: 2005-09-09 00:00:00 -06:00
6
+ version: 1.1.0
7
+ date: 2005-10-21
8
8
  summary: Speeds up methods at the cost of memory
9
9
  require_paths:
10
10
  - lib
@@ -24,15 +24,13 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
24
24
  version: 0.0.0
25
25
  version:
26
26
  platform: ruby
27
- signing_key:
28
- cert_chain:
29
27
  authors:
30
28
  - Daniel J. Berger
31
29
  files:
32
30
  - lib/memoize.rb
31
+ - README
33
32
  - CHANGES
34
33
  - MANIFEST
35
- - README
36
34
  - test/tc_memoize.rb
37
35
  test_files:
38
36
  - test/tc_memoize.rb