certainty 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.travis.yml +8 -0
- data/Gemfile +6 -0
- data/README.md +12 -0
- data/Rakefile +9 -0
- data/certainty.gemspec +20 -0
- data/lib/certainty/version.rb +3 -0
- data/lib/certainty.rb +59 -0
- data/test/certainty_test.rb +39 -0
- metadata +61 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
data/certainty.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "certainty/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "certainty"
|
7
|
+
s.version = Certainty::VERSION
|
8
|
+
s.authors = ["Hakan Ensari"]
|
9
|
+
s.email = ["code@papercavalier.com"]
|
10
|
+
s.homepage = "http://github.com/hakanensari/certainty"
|
11
|
+
s.summary = %q{An object that represents truth}
|
12
|
+
s.description = %q{Certainty sneaks Boolean, an object that represents truth, into Ruby.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "certainty"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
data/lib/certainty.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# = Certainty
|
2
|
+
#
|
3
|
+
# Sneaks +Boolean+, an object that represents truth, into Ruby.
|
4
|
+
module Boolean; end
|
5
|
+
|
6
|
+
[TrueClass, FalseClass].each do |klass|
|
7
|
+
klass.send :include, Boolean
|
8
|
+
end
|
9
|
+
|
10
|
+
class Object
|
11
|
+
# Returns +nil+. The truth of an object, by default, is not knowable.
|
12
|
+
def to_bool
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
alias to_b to_bool
|
16
|
+
end
|
17
|
+
|
18
|
+
module Boolean
|
19
|
+
# Returns the receiver. The truth of a boolean object is
|
20
|
+
# self-referential.
|
21
|
+
def to_bool
|
22
|
+
self
|
23
|
+
end
|
24
|
+
alias to_b to_bool
|
25
|
+
end
|
26
|
+
|
27
|
+
class String
|
28
|
+
# Returns +true+ if, downcased, the receiver is +"1"+ or +"true"+,
|
29
|
+
# +false+ if it is +"0"+ or +"false"+, or +nil+ otherwise.
|
30
|
+
def to_bool
|
31
|
+
{
|
32
|
+
'1' => true,
|
33
|
+
'true' => true,
|
34
|
+
'0' => false,
|
35
|
+
'false' => false
|
36
|
+
}[downcase]
|
37
|
+
end
|
38
|
+
alias to_b to_bool
|
39
|
+
end
|
40
|
+
|
41
|
+
class Numeric
|
42
|
+
# Returns + true+ if the receiver, converted to float, is equal to
|
43
|
+
# +1.0+, +false+ if it is equal +0.0+, or +nil+ otherwise.
|
44
|
+
def to_bool
|
45
|
+
{
|
46
|
+
1.0 => true,
|
47
|
+
0.0 => false
|
48
|
+
}[to_f]
|
49
|
+
end
|
50
|
+
alias to_b to_bool
|
51
|
+
end
|
52
|
+
|
53
|
+
module Kernel
|
54
|
+
# Converts _arg_ to a boolean object or +nil+ if its truth is not
|
55
|
+
# knowable.
|
56
|
+
def Boolean(arg)
|
57
|
+
arg.to_bool
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
$:.push File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
|
6
|
+
require 'certainty'
|
7
|
+
require 'test/unit'
|
8
|
+
|
9
|
+
class TestCertainty < Test::Unit::TestCase
|
10
|
+
def test_existence
|
11
|
+
assert_kind_of Boolean, true
|
12
|
+
assert_kind_of Boolean, false
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_boolean_representation
|
16
|
+
assert_equal true, true.to_bool
|
17
|
+
assert_equal false, false.to_b
|
18
|
+
|
19
|
+
assert_equal true, 'true'.to_b
|
20
|
+
assert_equal true, '1'.to_b
|
21
|
+
assert_equal false, 'FALSE'.to_b
|
22
|
+
assert_equal false, '0'.to_b
|
23
|
+
assert_nil 'foo'.to_b
|
24
|
+
|
25
|
+
assert_equal true, 1.to_b
|
26
|
+
assert_equal false, 0.0.to_b
|
27
|
+
assert_nil 2.to_b
|
28
|
+
|
29
|
+
assert_nil nil.to_b
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_conversion
|
33
|
+
assert_equal true, Boolean(true)
|
34
|
+
assert_equal true, Boolean('1')
|
35
|
+
assert_equal false, Boolean(false)
|
36
|
+
assert_equal false, Boolean('0')
|
37
|
+
assert_equal nil, Boolean('foo')
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: certainty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Hakan Ensari
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-20 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Certainty sneaks Boolean, an object that represents truth, into Ruby.
|
15
|
+
email:
|
16
|
+
- code@papercavalier.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .travis.yml
|
23
|
+
- Gemfile
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- certainty.gemspec
|
27
|
+
- lib/certainty.rb
|
28
|
+
- lib/certainty/version.rb
|
29
|
+
- test/certainty_test.rb
|
30
|
+
homepage: http://github.com/hakanensari/certainty
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
hash: 2654129636937927522
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
hash: 2654129636937927522
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project: certainty
|
56
|
+
rubygems_version: 1.8.6
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: An object that represents truth
|
60
|
+
test_files:
|
61
|
+
- test/certainty_test.rb
|