/b001e

Message-sending based re-implementation of the Boolean operators.

Primary LanguageRubyMIT LicenseMIT

B001e

A message-sending based re-implementation of Boolean operators in pure Ruby

Abstract

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.

Operator / keyword style:

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 yielded 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.

The RubySpec license is also MIT X11.