anything 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +74 -0
- data/Rakefile +44 -0
- data/lib/anything.rb +9 -0
- data/test/test_anything.rb +39 -0
- metadata +58 -0
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
Anything
|
2
|
+
========
|
3
|
+
|
4
|
+
Anything is a simple Ruby object with two significant facets to its behaviour:
|
5
|
+
|
6
|
+
1. `Anything === a` is true for all values of `a`.
|
7
|
+
2. `Anything == a` is also true for all values of `a`.
|
8
|
+
|
9
|
+
It therefore follows that:
|
10
|
+
|
11
|
+
case a
|
12
|
+
when Anything
|
13
|
+
:always
|
14
|
+
else
|
15
|
+
:never
|
16
|
+
end
|
17
|
+
|
18
|
+
will always evaluate to `:always`, and that:
|
19
|
+
|
20
|
+
case [a, b]
|
21
|
+
when ["X", Anything]
|
22
|
+
:x
|
23
|
+
when [Anything, "Y"]
|
24
|
+
:y
|
25
|
+
else
|
26
|
+
:z
|
27
|
+
end
|
28
|
+
|
29
|
+
is equivalent to:
|
30
|
+
|
31
|
+
if a == "X"
|
32
|
+
:x
|
33
|
+
elsif b == "Y"
|
34
|
+
:y
|
35
|
+
else
|
36
|
+
:z
|
37
|
+
end
|
38
|
+
|
39
|
+
These properties let us take nested code like:
|
40
|
+
|
41
|
+
case platform
|
42
|
+
when "linux"
|
43
|
+
case architecture
|
44
|
+
when /i\d86/
|
45
|
+
"linux_x86"
|
46
|
+
when "x86_64"
|
47
|
+
"linux_amd64"
|
48
|
+
else
|
49
|
+
raise "unsupported architecture"
|
50
|
+
end
|
51
|
+
when "darwin"
|
52
|
+
"mac"
|
53
|
+
else
|
54
|
+
raise "unsupported platform"
|
55
|
+
end
|
56
|
+
|
57
|
+
and rewrite it using a pattern-matching idiom:
|
58
|
+
|
59
|
+
require "anything"
|
60
|
+
|
61
|
+
case [platform, architecture]
|
62
|
+
when ["linux", /i\d86/]
|
63
|
+
"linux_32_bit"
|
64
|
+
when ["linux", "x86_64"]
|
65
|
+
"linux_64_bit"
|
66
|
+
when ["linux", Anything]
|
67
|
+
raise "unsupported architecture"
|
68
|
+
when ["darwin", Anything]
|
69
|
+
"mac"
|
70
|
+
else
|
71
|
+
raise "unsupported platform"
|
72
|
+
end
|
73
|
+
|
74
|
+
Whether it's actually useful or not is left to you to decide.
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rake/gempackagetask"
|
3
|
+
require "rake/testtask"
|
4
|
+
|
5
|
+
task :default => [:test]
|
6
|
+
|
7
|
+
Rake::TestTask.new("test") do |t|
|
8
|
+
t.libs << "test"
|
9
|
+
t.pattern = "test/**/test_*.rb"
|
10
|
+
t.verbose = true
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :test
|
14
|
+
|
15
|
+
require "rake/testtask"
|
16
|
+
Rake::TestTask.new do |t|
|
17
|
+
t.libs << "test"
|
18
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
19
|
+
t.verbose = true
|
20
|
+
end
|
21
|
+
|
22
|
+
spec = Gem::Specification.new do |s|
|
23
|
+
s.name = "anything"
|
24
|
+
s.version = "0.0.1"
|
25
|
+
s.summary = "An object that matches anything. May be useful for pattern matching in case statements."
|
26
|
+
s.author = "Paul Battley"
|
27
|
+
s.email = "pbattley@gmail.com"
|
28
|
+
|
29
|
+
s.has_rdoc = false
|
30
|
+
|
31
|
+
s.files = %w(Rakefile README.md) + Dir.glob("{bin,test,lib}/**/*")
|
32
|
+
s.executables = FileList["bin/**"].map { |f| File.basename(f) }
|
33
|
+
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
end
|
36
|
+
|
37
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
38
|
+
pkg.gem_spec = spec
|
39
|
+
end
|
40
|
+
|
41
|
+
desc 'Clear out generated packages'
|
42
|
+
task :clean => [:clobber_package] do
|
43
|
+
rm "#{spec.name}.gemspec"
|
44
|
+
end
|
data/lib/anything.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
require "test/unit"
|
3
|
+
require "anything"
|
4
|
+
|
5
|
+
class AnythingTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def test_should_match_in_case_when_alone
|
8
|
+
actual =
|
9
|
+
case "foo"
|
10
|
+
when Anything
|
11
|
+
:expected
|
12
|
+
else
|
13
|
+
:unexpected
|
14
|
+
end
|
15
|
+
assert_equal :expected, actual
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_match_in_case_when_in_array
|
19
|
+
actual =
|
20
|
+
case ["foo", "bar"]
|
21
|
+
when ["foo", Anything]
|
22
|
+
:expected
|
23
|
+
else
|
24
|
+
:unexpected
|
25
|
+
end
|
26
|
+
assert_equal :expected, actual
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_match_in_case_when_in_nested_array
|
30
|
+
actual =
|
31
|
+
case ["foo", ["bar", "baz"]]
|
32
|
+
when ["foo", ["bar", Anything]]
|
33
|
+
:expected
|
34
|
+
else
|
35
|
+
:unexpected
|
36
|
+
end
|
37
|
+
assert_equal :expected, actual
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: anything
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Battley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-04 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: pbattley@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- README.md
|
27
|
+
- test/test_anything.rb
|
28
|
+
- lib/anything.rb
|
29
|
+
has_rdoc: true
|
30
|
+
homepage:
|
31
|
+
licenses: []
|
32
|
+
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.3.5
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: An object that matches anything. May be useful for pattern matching in case statements.
|
57
|
+
test_files: []
|
58
|
+
|