B001e is a message-sending based re-implementation of the Boolean operators and
, or
and not
and the Boolean expressions if
and unless
in pure Ruby. Lazy Evaluation / Short-circuiting is achieved through the use of blocks and lambda expressions.
This library contains sample re-implementations of the
Boolean operators and
, or
and
not
and the Boolean expressions
if
and unless
, 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.
if c1 && c2
t
elsif c3
ei
else
e
end
becomes:
c1.and { c2 }.ifelse ->{ t }, ->{ c3.ifelse ->{ ei }, ->{ e } }
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 and
and or
are still
builtin operators and if
and unless
still builtin keywords.
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!
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.
At GitHub, of course!
gem install b001e
require 'b001e'
true.and { nil.or { 42 } }.if { puts "It's true!" }
# Equivalent to: if true && (nil || 42) then puts "It's true!" end
false.ifelse ->{ puts "You'll never see this." }, ->{ puts 'But this!' }
# Equivalent to: if false then puts "You'll never see this." else puts 'But this!' end
The API style is heavily influenced by Smalltalk.
The implementation is literally textbook: every introductory CS text should have it.
The Specs were directly lifted from the RubySpec project.
My original work is licensed under the MIT X11 License .
Copyright (c) 2009 Jörg W Mittag < JoergWMittag+B001e@GoogleMail.Com >
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The RubySpec license is also MIT X11.
The specs were originally ported and heavily modified from the RubySpec project < http://RubySpec.Org/ >. This is the original license:
Copyright (c) 2008 Engine Yard, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.