case 0.3 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/case.rb +36 -39
- data/lib/case/core.rb +17 -0
- data/test/test_all.rb +1 -0
- data/test/test_case.rb +14 -0
- metadata +4 -2
data/Rakefile
CHANGED
data/lib/case.rb
CHANGED
@@ -7,61 +7,58 @@
|
|
7
7
|
|
8
8
|
module Case
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
module Impl
|
11
|
+
module Struct
|
12
|
+
def ===(obj)
|
13
|
+
return false unless self.class === obj
|
14
|
+
zip(obj) { |e,o| return false unless e === o }
|
15
|
+
true
|
16
|
+
end
|
16
17
|
end
|
17
|
-
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
module Array
|
20
|
+
def ===(obj)
|
21
|
+
return false unless ::Array === obj
|
22
|
+
zip(obj) { |e,o| return false unless e === o }
|
23
|
+
true
|
24
|
+
end
|
23
25
|
end
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
class Operator
|
28
|
+
class << self
|
29
|
+
alias_method :[], :new
|
30
|
+
end
|
28
31
|
|
29
|
-
|
30
|
-
|
32
|
+
def initialize(*patterns)
|
33
|
+
@patterns = patterns
|
34
|
+
end
|
35
|
+
|
36
|
+
def ===(obj)
|
37
|
+
raise NotImplementedError, "#{self.class}#=== not implemented"
|
38
|
+
end
|
31
39
|
end
|
32
40
|
end
|
33
41
|
|
34
|
-
|
35
|
-
|
36
|
-
class << self
|
37
|
-
alias_method :[], :new
|
38
|
-
end
|
39
|
-
|
40
|
-
def initialize(*patterns)
|
41
|
-
@patterns = patterns
|
42
|
-
end
|
42
|
+
class Any < Impl::Operator
|
43
|
+
def self.===(obj) ; true ; end
|
43
44
|
|
44
45
|
def ===(obj)
|
45
|
-
@patterns.
|
46
|
+
@patterns.any? { |p| p === obj }
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
return false unless ::Tuple === other and size == other.size
|
53
|
-
to_a.zip(other) { |a, b| return false unless a === b }
|
54
|
-
true
|
50
|
+
class All < Impl::Operator
|
51
|
+
def ===(obj)
|
52
|
+
@patterns.all? { |p| p === obj }
|
55
53
|
end
|
56
54
|
end
|
55
|
+
|
56
|
+
class Struct < ::Struct
|
57
|
+
include Impl::Struct
|
57
58
|
end
|
58
59
|
|
59
60
|
class Array < ::Array
|
60
|
-
|
61
|
-
return false unless ::Array === other and size == other.size
|
62
|
-
zip(other) { |a, b| return false unless a === b }
|
63
|
-
true
|
64
|
-
end
|
61
|
+
include Impl::Array
|
65
62
|
end
|
66
63
|
|
67
64
|
class Guard
|
@@ -69,8 +66,8 @@ class Guard
|
|
69
66
|
@predicate = predicate
|
70
67
|
end
|
71
68
|
|
72
|
-
def ===(
|
73
|
-
@predicate.call
|
69
|
+
def ===(obj)
|
70
|
+
@predicate.call obj
|
74
71
|
end
|
75
72
|
end
|
76
73
|
|
data/lib/case/core.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# case/core.rb - pattern matching for Ruby (core supplement)
|
2
|
+
#
|
3
|
+
# Copyright 2008 MenTaLguY <mental@rydia.net>
|
4
|
+
#
|
5
|
+
# This library is made available under the same terms as Ruby.
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'case'
|
9
|
+
|
10
|
+
class Struct
|
11
|
+
include Case::Impl::Struct
|
12
|
+
end
|
13
|
+
|
14
|
+
class Array
|
15
|
+
include Case::Impl::Array
|
16
|
+
end
|
17
|
+
|
data/test/test_all.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
require 'test/test_case.rb'
|
data/test/test_case.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'case'
|
3
|
+
require 'case/core'
|
4
|
+
|
5
|
+
class CaseTest < Test::Unit::TestCase
|
6
|
+
def test_struct
|
7
|
+
[ ::Struct, Case::Struct ].each do |s|
|
8
|
+
c = s.new :foo, :bar, :baz
|
9
|
+
assert c[1, 2, Object] === c[1, 2, 3]
|
10
|
+
assert !( c[1, 2, 3] === c[1, 2, Object] )
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: case
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "0.
|
7
|
-
date: 2008-01-
|
6
|
+
version: "0.4"
|
7
|
+
date: 2008-01-22 00:00:00 -05:00
|
8
8
|
summary: Pattern matching for Ruby
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -30,7 +30,9 @@ authors:
|
|
30
30
|
files:
|
31
31
|
- Rakefile
|
32
32
|
- test/test_all.rb
|
33
|
+
- test/test_case.rb
|
33
34
|
- lib/case.rb
|
35
|
+
- lib/case/core.rb
|
34
36
|
test_files:
|
35
37
|
- test/test_all.rb
|
36
38
|
rdoc_options: []
|