-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
executable file
·79 lines (67 loc) · 1.22 KB
/
test.rb
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
#!/usr/bin/env ruby
class Score < Array
def initialize(v = 0)
super()
if !v.kind_of?(Fixnum) && !v.kind_of?(Float)
self[0] = 0.0
self[1] = 0.0
elsif v.abs > 1
self[0] = 0.0
self[1] = 0.0
else
self[0] = v.to_f
self[1] = 1.0
end
end
def modify(other,sign = 1)
if other.kind_of?(Array)
if self[1] <= 0
v = [sign*other[0].to_f,sign*other[1].to_f]
elsif other[1] <= 0
v = self
else
v = [self[0]+sign*other[0].to_f,self[1]+sign*other[1].to_f]
end
return self if v[1] < 0 || v[0].abs > v[1]
s = Score.new
s[0] = v[0]
s[1] = v[1]
return s
else
v = self + [sign*other.to_f,sign*(1.to_f)]
return self + [sign*other.to_f,sign*(1.to_f)]
end
end
def +(other)
v = modify(other)
return v
end
def -(other)
modify(other,-1)
end
def /(other)
v = self.dup
v[0] = self[0]/other.to_f
v
end
def <<(a)
v = self + a
self[0] = v[0]
self[1] = v[1]
self
end
def eval
return 0 if self[1] <= 0 || self[1] < self[0]
return self[0].to_f/self[1]
end
end
s = Score.new(0.3)
p s
puts
s << -1
p s
puts
s /= 4
p s
puts
puts s.eval