-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
121 lines (89 loc) · 2.36 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
NAME
methodhash
SYNOPSIS
A Hash subclass for automatic storage of values obtained from a method
defined by the user. Useful for lengthy calculations on large datasets.
URI
http://rubyforge.org/projects/methodhash
http://github.com/fredrikj/methodhash
INSTALL
gem install methodhash
DESCRIPTION
A Hash subclass that defines its values from a specified method.
Use it by creating a subclass of MethodHash, and define a method
with the name "mymethod" in it. Like this (same code in samples/samples.rb):
# 1. Simple use
class AddOne < MethodHash
def mymethod(a)
sleep 3
a + 1
end
end
a = AddOne.new
a # {}
a[1] # 2
a[7] # 8
a # {1=>2, 7=>8}
puts a.dump # --- !map:AddOne
# 1: 2
# 7: 8
# 2. With a file
b = AddOne.new '/tmp/one.yml'
b # {}
b[1] # 2
b.dump # '/tmp/one.yml'
c = AddOne.new '/tmp/one.yml'
puts c.inspect # {1=>2}
# 3. Some protection against data corruption.
class AddTwo < MethodHash
def mymethod(a)
a + 2
end
end
begin
d = AddTwo.new '/tmp/one.yml' # ArgumentError: Path holds class AddOne
rescue
puts $!
end
# 4. Saving exceptions arising from mymethod.
class AddOneFaulty < MethodHash
def mymethod(a)
rand(2)==0 ? raise("Epic Fail!") : a+1
end
end
e = AddOneFaulty.new
e[1] # RuntimeError: Epic Fail! # If something bad happened
e # {1=>"ERROR: Epic Fail!"}
e.retry_errors # false
e[1] # RuntimeError: Epic Fail! # Still keeping the error
e.retry_errors=true # true
e[1] # 2 # If better luck this time
e # {1=>2}
# 5. A more complex setting
class AddThree < MethodHash
def initialize(path1=nil, path2=nil, mypath=nil)
@one = AddOne.new(path1)
@two = AddTwo.new(path2)
super(mypath)
end
def mymethod(a)
@one[a] + @two[a] - a
end
def dump
@one.dump
@two.dump
super
end
end
f = AddThree.new( '/tmp/one.yml', '/tmp/two.yml')
puts f[3]
f.dump
# 6. With two arguments
class Add < MethodHash
def mymethod(a,b)
a + b
end
end
HISTORY
0.5.0
Initial version