b001e 0.0.0a

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,136 @@
1
+ =======
2
+ B001e
3
+ =======
4
+ ---------------------------------------------------------------------------
5
+ A message-sending based re-implementation of Boolean operators in pure Ruby
6
+ ---------------------------------------------------------------------------
7
+
8
+ .. admonition:: Abstract
9
+
10
+ B001e_ is a message-sending based re-implementation of the
11
+ Boolean operators ``and``, ``or`` and ``not`` and the Boolean
12
+ expressions ``if`` and ``unless`` in pure Ruby.
13
+ Lazy Evaluation / Short-circuiting is achieved through the use
14
+ of blocks and lambda expressions.
15
+
16
+ .. _B001e: https://JoergWMittag.GitHub.Com/b001e/
17
+
18
+ .. contents::
19
+
20
+ What
21
+ ====
22
+
23
+ This library contains sample re-implementations of the Boolean
24
+ operators ``and``, ``or`` and ``not`` and the Boolean expressions
25
+ ``if`` and ``unless``, in pure Ruby. The style is heavily
26
+ inspired by Smalltalk_ and its relatives: the operators become
27
+ messages that take block parameters and are sent to the
28
+ conditionals.
29
+
30
+ Operator / keyword style::
31
+
32
+ if c1 && c2
33
+ t
34
+ elsif c3
35
+ ei
36
+ else
37
+ e
38
+ end
39
+
40
+ becomes::
41
+
42
+ c1.and { c2 }.ifelse ->() { t }, ->() { c3.ifelse ->() { ei }, ->() { e } }
43
+
44
+ .. _Smalltalk: http://Smalltalk.Org/
45
+
46
+ Why
47
+ ===
48
+
49
+ Every so often, there is a discussion on either the Ruby-Talk_ or
50
+ Ruby-Core_ mailinglists, whether the number of operators that are
51
+ not backed by methods should be reduced. In Ruby 1.9, ``!`` and
52
+ ``!=`` have already become methods, but ``and`` and ``or`` are
53
+ still builtin operators and ``if`` and ``unless`` still builtin
54
+ keywords.
55
+
56
+ One argument that is sometimes brought up is that because of the
57
+ short-circuiting nature of those operators, implementing them as
58
+ methods is impossible or at least hard. I just wanted to see
59
+ *how* hard it really is!
60
+
61
+ .. _Ruby-Talk: http://Ruby-Forum.Com/forum/4/
62
+ .. _Ruby-Core: http://Ruby-Forum.Com/forum/14/
63
+
64
+ How
65
+ ===
66
+
67
+ All the operators become methods. The logic is achieved through
68
+ polymorphism: basically, ``NilClass`` and ``FalseClass`` get one
69
+ set of implementations, ``Object`` gets the opposite set.
70
+
71
+ Lazy Evaluation is achieved with blocks: if a block is not
72
+ supposed to be evaluated, it is simply never ``yield``\ ed to.
73
+
74
+ Where
75
+ =====
76
+
77
+ At `GitHub <https://GitHub.Com/JoergWMittag/B001e/>`_, of course!
78
+
79
+ Installation
80
+ ============
81
+
82
+ ::
83
+
84
+ gem install JoergWMittag-b001e
85
+
86
+ Usage
87
+ =====
88
+
89
+ ::
90
+
91
+ begin require 'rubygems'; rescue LoadError
92
+ else begin gem 'JoergWMittag-b001e', '~> 0.0.3'; rescue Gem::LoadError; end end
93
+ require 'b001e'
94
+
95
+ true.and { nil.or { 42 } }.if { puts "It's true!" }
96
+ # Equivalent to: if true && (nil || 42) then puts "It's true!" end
97
+
98
+ false.ifelse ->() { puts "You'll never see this." }, ->() { puts 'But this!' }
99
+ # Equivalent to: if false then puts "You'll never see this." else puts 'But this!' end
100
+
101
+ Acknowledgements
102
+ ================
103
+
104
+ Style
105
+ -----
106
+
107
+ The API style is heavily influenced by Smalltalk_.
108
+
109
+ Implementation
110
+ --------------
111
+
112
+ The implementation is literally textbook: every introductory CS
113
+ text should have it.
114
+
115
+ Specs
116
+ -----
117
+
118
+ The Specs were directly lifted from the RubySpec_ project.
119
+
120
+ .. _RubySpec: http://RubySpec.Org/
121
+
122
+ License
123
+ =======
124
+
125
+ My original work is licensed under the `MIT X11 License`_.
126
+
127
+ .. include:: LICENSE.txt
128
+ :literal:
129
+
130
+ The `RubySpec license`_ is also MIT X11.
131
+
132
+ .. include:: spec/LICENSE.txt
133
+ :literal:
134
+
135
+ .. _MIT X11 License: https://GitHub.Com/JoergWMittag/B001e/tree/master/LICENSE.txt
136
+ .. _RubySpec license: https://GitHub.Com/RubySpec/RubySpec/tree/master/LICENSE
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
5
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
6
+
7
+ require 'rake'
8
+
9
+ taskdir = File.expand_path File.join(File.dirname(__FILE__), 'tasks')
10
+ $LOAD_PATH.unshift taskdir unless $LOAD_PATH.include? taskdir
11
+
12
+ FileList[File.join taskdir, '**', '*_task.rb'].each { |raketask| load raketask }
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
5
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
6
+
7
+ filelist = %w[
8
+ b001e.gemspec
9
+ lib/b001e/extensions/core/false_class_extensions.rb
10
+ lib/b001e/extensions/core/nil_class_extensions.rb
11
+ lib/b001e/extensions/core/object_extensions.rb
12
+ lib/b001e/extensions/core/true_class_extensions.rb
13
+ lib/b001e/falsiness.rb
14
+ lib/b001e/truthiness.rb
15
+ lib/b001e.rb
16
+ LICENSE.txt
17
+ Rakefile.rb
18
+ README.rst
19
+ spec/and_spec.rb
20
+ spec/b001e_suite.rb
21
+ spec/if_spec.rb
22
+ spec/LICENSE.txt
23
+ spec/not_spec.rb
24
+ spec/or_spec.rb
25
+ spec/spec_helper.rb
26
+ spec/unless_spec.rb
27
+ tasks/gem_task.rb
28
+ tasks/spec_task.rb
29
+ tasks/_default_task.rb
30
+ ]
31
+
32
+ speclist = filelist.grep /^spec/
33
+
34
+ $spec = Gem::Specification.new do |s|
35
+ s.name = 'b001e'
36
+ s.summary = 'Message-sending based re-implementation of the Boolean operators.'
37
+ s.version = Gem::Version.new '0.0.0a'
38
+ s.author = 'Jörg W Mittag'
39
+ s.email = 'JoergWMittag+B001e@GoogleMail.Com'
40
+ s.homepage = 'http://JoergWMittag.GitHub.Com/b001e/'
41
+ s.rubyforge_project = 'b001e'
42
+ s.license = 'MIT X11'
43
+ s.required_ruby_version = '~> 1.9.1'
44
+ s.required_rubygems_version = '~> 1.3.5'
45
+ s.has_rdoc = true
46
+ s.rdoc_options = %w[--all --charset=UTF-8 --line-numbers --webcvs=https://GitHub.Com/JoergWMittag/B001e/blob/master/%s]
47
+ s.extra_rdoc_files = %w[LICENSE.txt README.rst]
48
+ s.files = filelist
49
+ s.test_files = speclist
50
+ s.description = <<-'HERE'
51
+ B001e is a message-sending based re-implementation of the
52
+ Boolean operators 'if', 'unless', 'and', 'or' and 'not' in
53
+ pure Ruby. Lazy Evaluation / Short-circuiting is achieved
54
+ through the use of blocks and lambda expressions.
55
+ HERE
56
+ end
57
+
58
+ if __FILE__ == $0
59
+ Gem::manage_gems
60
+ Gem::Builder.new($spec).build
61
+ end
@@ -0,0 +1,12 @@
1
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
2
+
3
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
4
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
5
+
6
+ libdir = File.expand_path(File.dirname __FILE__).gsub(/(.*lib).*?/, '\1')
7
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
8
+
9
+ require 'b001e/extensions/core/object_extensions'
10
+ require 'b001e/extensions/core/true_class_extensions'
11
+ require 'b001e/extensions/core/false_class_extensions'
12
+ require 'b001e/extensions/core/nil_class_extensions'
@@ -0,0 +1,23 @@
1
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
2
+
3
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
4
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
5
+
6
+ libdir = File.expand_path(File.dirname __FILE__).gsub(/(.*lib).*?/, '\1')
7
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
8
+
9
+ require 'b001e/falsiness'
10
+
11
+ module B001e
12
+ module Extensions
13
+ module Core
14
+ module FalseClassExtensions
15
+ include B001e::Falsiness
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class FalseClass
22
+ include B001e::Extensions::Core::FalseClassExtensions
23
+ end
@@ -0,0 +1,23 @@
1
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
2
+
3
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
4
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
5
+
6
+ libdir = File.expand_path(File.dirname __FILE__).gsub(/(.*lib).*?/, '\1')
7
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
8
+
9
+ require 'b001e/falsiness'
10
+
11
+ module B001e
12
+ module Extensions
13
+ module Core
14
+ module NilClassExtensions
15
+ include B001e::Falsiness
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class NilClass
22
+ include B001e::Extensions::Core::NilClassExtensions
23
+ end
@@ -0,0 +1,23 @@
1
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
2
+
3
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
4
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
5
+
6
+ libdir = File.expand_path(File.dirname __FILE__).gsub(/(.*lib).*?/, '\1')
7
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
8
+
9
+ require 'b001e/truthiness'
10
+
11
+ module B001e
12
+ module Extensions
13
+ module Core
14
+ module ObjectExtensions
15
+ include B001e::Truthiness
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class Object
22
+ include B001e::Extensions::Core::ObjectExtensions
23
+ end
@@ -0,0 +1,23 @@
1
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
2
+
3
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
4
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
5
+
6
+ libdir = File.expand_path(File.dirname __FILE__).gsub(/(.*lib).*?/, '\1')
7
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
8
+
9
+ require 'b001e/truthiness'
10
+
11
+ module B001e
12
+ module Extensions
13
+ module Core
14
+ module TrueClassExtensions
15
+ include B001e::Truthiness
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class TrueClass
22
+ include B001e::Extensions::Core::TrueClassExtensions
23
+ end
@@ -0,0 +1,38 @@
1
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
2
+
3
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
4
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
5
+
6
+ libdir = File.expand_path(File.dirname __FILE__).gsub(/(.*lib).*?/, '\1')
7
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
8
+
9
+ module B001e
10
+ module Falsiness
11
+ def if
12
+ end
13
+
14
+ def ifelse _ = nil, else_branch = ->() {}
15
+ else_branch.call
16
+ end
17
+
18
+ def unless
19
+ yield
20
+ end
21
+
22
+ def unlesselse unless_branch = ->() {}, _ = nil
23
+ ifelse _, unless_branch
24
+ end
25
+
26
+ def and
27
+ self
28
+ end
29
+
30
+ def or
31
+ yield
32
+ end
33
+
34
+ def not
35
+ true
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
2
+
3
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
4
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
5
+
6
+ libdir = File.expand_path(File.dirname __FILE__).gsub(/(.*lib).*?/, '\1')
7
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
8
+
9
+ module B001e
10
+ module Truthiness
11
+ def if
12
+ yield
13
+ end
14
+
15
+ def ifelse then_branch = ->() {}, _ = nil
16
+ then_branch.call
17
+ end
18
+
19
+ def unless
20
+ end
21
+
22
+ def unlesselse _ = nil, else_branch = ->() {}
23
+ ifelse else_branch, _
24
+ end
25
+
26
+ def and
27
+ yield
28
+ end
29
+
30
+ def or
31
+ self
32
+ end
33
+
34
+ def not
35
+ false
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ The specs were originally ported and heavily modified from the
2
+ RubySpec project <http://RubySpec.Org/>. This is the original
3
+ license:
4
+
5
+ Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
6
+
7
+ Permission is hereby granted, free of charge, to any person
8
+ obtaining a copy of this software and associated documentation
9
+ files (the "Software"), to deal in the Software without
10
+ restriction, including without limitation the rights to use,
11
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the
13
+ Software is furnished to do so, subject to the following
14
+ conditions:
15
+
16
+ The above copyright notice and this permission notice shall be
17
+ included in all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ #Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
5
+ #This code was directly ported from <https://GitHub.Com/RubySpec/RubySpec/tree/master/language/and_spec.rb>
6
+
7
+ require File.expand_path File.join(File.dirname(__FILE__), 'spec_helper')
8
+
9
+ require 'b001e'
10
+
11
+ describe "The 'and' operator" do
12
+ it 'short-circuits evaluation at the first condition to be false' do
13
+ x = nil
14
+ true.and { false.and { x = 1 } }
15
+ x.must_be_nil
16
+ end
17
+
18
+ it 'evaluates to the first condition not to be true' do
19
+ (:yes.and { 1.and { nil.and { true } } }).must_be_nil
20
+ (:yes.and { 1.and { false.and { true } } }).must_equal false
21
+ end
22
+
23
+ it 'evaluates to the last condition if all are true' do
24
+ (:yes.and { 1 }).must_equal 1
25
+ (1.and { :yes }).must_equal :yes
26
+ end
27
+
28
+ it 'evaluates the full set of chained conditions during assignment' do
29
+ x, y = nil
30
+ x = 1.and { y = 2 }
31
+ # "1 && y = 2" is evaluated and then assigned to x
32
+ x.must_equal 2
33
+ end
34
+
35
+ it 'when used in assignment, evaluates and assigns expressions individually' do
36
+ x, y = nil
37
+ (x = 1).and { y = 2 }
38
+ # evaluates (x=1) and (y=2)
39
+ x.must_equal 1
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
5
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
6
+
7
+ specdir = File.expand_path(File.dirname __FILE__).gsub(/(.*spec).*?/, '\1')
8
+ $LOAD_PATH.unshift specdir unless $LOAD_PATH.include? specdir
9
+
10
+ require 'spec_helper'
11
+
12
+ Dir[File.join specdir, '**', '*_spec.rb'].each { |spec| require spec }
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ #Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
5
+ #This code was directly ported from <https://GitHub.Com/RubySpec/RubySpec/tree/master/language/if_spec.rb>
6
+
7
+ require File.expand_path File.join(File.dirname(__FILE__), 'spec_helper')
8
+
9
+ require 'b001e'
10
+
11
+ describe 'The if expression' do
12
+ before do
13
+ @result = []
14
+ end
15
+
16
+ it 'evaluates body if expression is true' do
17
+ true.if { @result << 123 }
18
+ @result.must_equal [123]
19
+ end
20
+
21
+ it 'does not evaluate body if expression is false' do
22
+ false.if { @result << 123 }
23
+ @result.must_equal []
24
+ end
25
+
26
+ it 'does not evaluate else-body if expression is true' do
27
+ true.ifelse ->() { @result << 123 }, ->() { @result << 456 }
28
+ @result.must_equal [123]
29
+ end
30
+
31
+ it 'evaluates only else-body if expression is false' do
32
+ false.ifelse ->() { @result << 123 }, ->() { @result << 456 }
33
+ @result.must_equal [456]
34
+ end
35
+
36
+ it 'returns result of then-body evaluation if expression is true' do
37
+ true.if { 123 }.must_equal 123
38
+ end
39
+
40
+ it 'returns result of last statement in then-body if expression is true' do
41
+ true.if do
42
+ :foo
43
+ :bar
44
+ end.must_equal :bar
45
+ end
46
+
47
+ it 'returns result of then-body evaluation if expression is true and else part is present' do
48
+ true.ifelse(->() { 123 }, ->() { 456 }).must_equal 123
49
+ end
50
+
51
+ it 'returns result of else-body evaluation if expression is false' do
52
+ false.ifelse(->() { 123 }, ->() { 456 }).must_equal 456
53
+ end
54
+
55
+ it 'returns nil if then-body is empty and expression is true' do
56
+ true.if{}.must_be_nil
57
+ end
58
+
59
+ it 'returns nil if then-body is empty, expression is true and else part is present' do
60
+ true.ifelse(->() {}, ->() { 456 }).must_be_nil
61
+ end
62
+
63
+ it 'returns nil if then-body is empty, expression is true and else part is empty' do
64
+ true.ifelse.must_be_nil
65
+ end
66
+
67
+ it 'returns nil if else-body is empty and expression is false' do
68
+ false.ifelse(->() { 123 }).must_be_nil
69
+ end
70
+
71
+ it 'returns nil if else-body is empty, expression is false and then-body is empty' do
72
+ false.ifelse.must_be_nil
73
+ end
74
+
75
+ it 'considers an expression with nil result as false' do
76
+ nil.ifelse(->() { 123 }, ->() { 456 }).must_equal 456
77
+ end
78
+
79
+ it 'considers a non-nil and non-boolean object in expression result as true' do
80
+ MiniTest::Mock.new.ifelse(->() { 123 }, ->() { 456 }).must_equal 123
81
+ end
82
+
83
+ it 'considers a zero integer in expression result as true' do
84
+ 0.ifelse(->() { 123 }, ->() { 456 }).must_equal 123
85
+ end
86
+
87
+ it 'evaluates subsequent elsif statements and execute body of first matching' do
88
+ false.ifelse(->() { 123 }, ->() { false.ifelse ->() { 234 }, ->() { true.ifelse ->() { 345 }, ->() { true.if { 456 } } } }).must_equal 345
89
+ end
90
+
91
+ it 'evaluates else-body if no if/elsif statements match' do
92
+ false.ifelse(->() { 123 }, ->() { false.ifelse ->() { 234 }, ->() { false.ifelse ->() { 345 }, ->() { 456 } } }).must_equal 456
93
+ end
94
+ end
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ #Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
5
+ #This code was directly ported from <https://GitHub.Com/RubySpec/RubySpec/tree/master/language/not_spec.rb>
6
+
7
+ require File.expand_path File.join(File.dirname(__FILE__), 'spec_helper')
8
+
9
+ require 'b001e'
10
+
11
+ describe "The 'not' operator" do
12
+ it 'turns false to true' do
13
+ false.not.must_equal true
14
+ end
15
+
16
+ it 'turns nil to true' do
17
+ nil.not.must_equal true
18
+ end
19
+
20
+ it 'turns true to false' do
21
+ true.not.must_equal false
22
+ end
23
+
24
+ it 'turns anything not nil to false' do
25
+ MiniTest::Mock.new.not.must_equal false
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ #Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
5
+ #This code was directly ported from <https://GitHub.Com/RubySpec/RubySpec/tree/master/language/or_spec.rb>
6
+
7
+ require File.expand_path File.join(File.dirname(__FILE__), 'spec_helper')
8
+
9
+ require 'b001e'
10
+
11
+ describe "The 'or' operator" do
12
+ it 'evaluates to true if any of its operands are true' do
13
+ (false.or { true.or { nil } }).if { @result = true }
14
+ @result.must_equal true
15
+ end
16
+
17
+ it 'evaluates to false if all of its operands are false' do
18
+ (false.or { nil }).if { @result = true }
19
+ @result.must_be_nil
20
+ end
21
+
22
+ it 'is evaluated before assignment operators' do
23
+ x = nil.or { true }
24
+ x.must_equal true
25
+ end
26
+
27
+ it 'is evaluated after variables are assigned' do
28
+ (x = nil).or { true }
29
+ x.must_be_nil
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
2
+
3
+ #Copyright (c) 2009 Jörg W Mittag <JoergWMittag+B001e@GoogleMail.Com>
4
+ #This code is licensed under the terms of the MIT License (see LICENSE.txt)
5
+
6
+ require 'minitest/spec'
7
+ require 'minitest/mock'
8
+ require 'minitest/autorun'
9
+
10
+ libdir = File.expand_path File.join(File.dirname(__FILE__), 'lib').gsub(/(.*)spec.*?/, '\1')
11
+ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include? libdir
12
+
13
+ specdir = File.expand_path(File.dirname __FILE__).gsub(/(.*spec).*?/, '\1')
14
+ $LOAD_PATH.unshift specdir unless $LOAD_PATH.include? specdir
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ #Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
5
+ #This code was directly ported from <https://GitHub.Com/RubySpec/RubySpec/tree/master/language/unless_spec.rb>
6
+
7
+ require File.expand_path File.join(File.dirname(__FILE__), 'spec_helper')
8
+
9
+ require 'b001e'
10
+
11
+ describe 'The unless expression' do
12
+ it 'evaluates the unless body when the expression is false' do
13
+ false.unlesselse ->() { @result = true }, ->() { @result = false }
14
+ @result.must_equal true
15
+ end
16
+
17
+ it 'returns the last statement in the body' do
18
+ false.unless do
19
+ :foo
20
+ :bar
21
+ end.must_equal :bar
22
+ end
23
+
24
+ it 'evaluates the else body when the expression is true' do
25
+ true.unlesselse(->() { :foo }, ->() { :bar }).must_equal :bar
26
+ end
27
+
28
+ it 'does not return a value when the expression is true' do
29
+ true.unless.must_be_nil
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ require 'rake'
5
+
6
+ taskdir = File.expand_path(File.dirname __FILE__).gsub(/(.*tasks).*?/, '\1')
7
+ $LOAD_PATH.unshift taskdir unless $LOAD_PATH.include? taskdir
8
+
9
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ require 'rake'
5
+ require 'rake/gempackagetask'
6
+
7
+ taskdir = File.expand_path(File.dirname __FILE__).gsub(/(.*tasks).*?/, '\1')
8
+ $LOAD_PATH.unshift taskdir unless $LOAD_PATH.include? taskdir
9
+
10
+ basedir = File.expand_path File.join(taskdir, '..')
11
+ $LOAD_PATH.unshift basedir unless $LOAD_PATH.include? basedir
12
+
13
+ load Dir[File.expand_path File.join(basedir, '*.gemspec')].first
14
+
15
+ Rake::GemPackageTask.new($spec) do |pkg|
16
+ pkg.need_tar = true
17
+ pkg.need_tar_gz = true
18
+ pkg.need_tar_bz2 = true
19
+ pkg.need_zip = true
20
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # vim: filetype=ruby, fileencoding=UTF-8, tabsize=2, shiftwidth=2
3
+
4
+ require 'rake'
5
+
6
+ taskdir = File.expand_path(File.dirname __FILE__).gsub(/(.*tasks).*?/, '\1')
7
+ $LOAD_PATH.unshift taskdir unless $LOAD_PATH.include? taskdir
8
+
9
+ desc 'Run the Specs'
10
+ task :spec do
11
+ FileList[File.join taskdir, '..', 'spec', '**', '*_suite.rb'].each { |specsuite| require specsuite }
12
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: b001e
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0a
5
+ platform: ruby
6
+ authors:
7
+ - "J\xC3\xB6rg W Mittag"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-18 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: |
17
+ B001e is a message-sending based re-implementation of the
18
+ Boolean operators 'if', 'unless', 'and', 'or' and 'not' in
19
+ pure Ruby. Lazy Evaluation / Short-circuiting is achieved
20
+ through the use of blocks and lambda expressions.
21
+
22
+ email: JoergWMittag+B001e@GoogleMail.Com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE.txt
29
+ - README.rst
30
+ files:
31
+ - b001e.gemspec
32
+ - lib/b001e/extensions/core/false_class_extensions.rb
33
+ - lib/b001e/extensions/core/nil_class_extensions.rb
34
+ - lib/b001e/extensions/core/object_extensions.rb
35
+ - lib/b001e/extensions/core/true_class_extensions.rb
36
+ - lib/b001e/falsiness.rb
37
+ - lib/b001e/truthiness.rb
38
+ - lib/b001e.rb
39
+ - LICENSE.txt
40
+ - Rakefile.rb
41
+ - README.rst
42
+ - spec/and_spec.rb
43
+ - spec/b001e_suite.rb
44
+ - spec/if_spec.rb
45
+ - spec/LICENSE.txt
46
+ - spec/not_spec.rb
47
+ - spec/or_spec.rb
48
+ - spec/spec_helper.rb
49
+ - spec/unless_spec.rb
50
+ - tasks/gem_task.rb
51
+ - tasks/spec_task.rb
52
+ - tasks/_default_task.rb
53
+ has_rdoc: true
54
+ homepage: http://JoergWMittag.GitHub.Com/b001e/
55
+ licenses:
56
+ - MIT X11
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --all
60
+ - --charset=UTF-8
61
+ - --line-numbers
62
+ - --webcvs=https://GitHub.Com/JoergWMittag/B001e/blob/master/%s
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.9.1
70
+ version:
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.5
76
+ version:
77
+ requirements: []
78
+
79
+ rubyforge_project: b001e
80
+ rubygems_version: 1.3.5
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Message-sending based re-implementation of the Boolean operators.
84
+ test_files:
85
+ - spec/and_spec.rb
86
+ - spec/b001e_suite.rb
87
+ - spec/if_spec.rb
88
+ - spec/LICENSE.txt
89
+ - spec/not_spec.rb
90
+ - spec/or_spec.rb
91
+ - spec/spec_helper.rb
92
+ - spec/unless_spec.rb