=======
Boole
A message-sending based re-implementation of Boolean operators in pure Ruby
.. admonition:: Abstract
Boole_ is a message-sending based re-implementation of the
Boolean operators ``if``, ``unless``, ``and``, ``or`` and
``not`` in pure Ruby. Lazy Evaluation / Short-circuiting is
achieved through the use of blocks and lambda expressions.
.. _Boole: https://JoergWMittag.GitHub.Com/Boole/
.. contents::
What
This library contains sample re-implementations of the Boolean
operators if
, unless
, and
, or
and not
in pure
Ruby. The style is heavily inspired by Smalltalk_ and its
relatives: the operators become messages that take block
parameters and are sent to the conditionals.
Operator / keyword style::
if condition1 && condition2
then_block
elsif condition3
elsif_block
else
else_block
end
becomes::
condition1.and { condition2 }.ifelse ->() { then_block }, ->() { condition3.ifelse ->() { elsif_block }, ->() { else_block } }
.. _Smalltalk: http://Smalltalk.Org/
Why
Every so often, there is a discussion on either the Ruby-Talk_ or
Ruby-Core_ mailinglists, whether the number of operators that are
not backed by methods should be reduced. In Ruby 1.9, !
and
!=
have already become methods, but if
, unless
,
and
and or
are still builtin operators.
One argument that is sometimes brought up is that because of the
short-circuiting nature of those operators, implementing them as
methods is impossible or at least hard. I just wanted to see
how hard it really is!
.. _Ruby-Talk: http://Ruby-Forum.Com/forum/4/
.. _Ruby-Core: http://Ruby-Forum.Com/forum/14/
How
All the operators become methods. The logic is achieved through
polymorphism: basically, NilClass
and FalseClass
get one
set of implementations, Object
gets the opposite set.
Lazy Evaluation is achieved with blocks: if a block is not
supposed to be evaluated, it is simply never yield
\ ed to.
Installation
::
gem install JoergWMittag-boole
Usage
::
begin require 'rubygems'; rescue LoadError
else begin gem 'JoergWMittag-boole', '~> 0.0.1'; rescue Gem::LoadError; end end
require 'boole'
true.and { nil.or { 42 } }.if { puts "It's true!" }
false.ifelse ->() { puts "You'll never see this." }, ->() { puts 'But this!' }
Acknowledgements
Style
The API style is heavily influenced by Smalltalk_.
Implementation
The implementation is literally textbook: every introductory CS
text should have it.
Specs
The Specs were directly lifted from the RubySpec_ project.
.. _RubySpec: http://RubySpec.Org/
License
My original work is licensed under the MIT X11 License
_.
.. include:: LICENSE.txt
:literal:
The RubySpec license
_ is also MIT X11.
.. include:: spec/LICENSE.txt
:literal:
.. _MIT X11 License: https://GitHub.Com/JoergWMittag/Boole/tree/master/LICENSE.txt
.. _RubySpec license: https://GitHub.Com/RubySpec/RubySpec/tree/master/LICENSE