The maze book for programmers!
mazesforprogrammers.com

Algorithms, circle mazes, hex grids, masking, weaving, braiding, 3D and 4D grids, spheres, and more!

DRM-Free Ebook

The Buckblog

assorted ramblings by Jamis Buck

Truth: Generating Truth Tables

11 June 2016 — A new library for generating truth tables is announced — 2-minute read

The post I was going to write today decided it needed some truth tables to support it, so of course (being the sort of programmer I am) I decided I would first write a utility to parse an expression and display the associated truth table.

(There are other tools out there that do this, some probably better than what I came up with, but I love writing parsers and I really just wanted to write my own. So there.)

So, as a result, I didn’t have time to write the post I was originally intending to write. That’s the bad news. The good news is that instead of that post, you get a post announcing my truth-table generator!

(And I still plan to write that other post. It’ll have to wait for next week, though.)

So, without further ado, allow me to introduce Truth!

There’s no gem for it (for various reasons, the most embarrassing being that the code has no tests yet…), and there are other gems called “truth”, and “truth-table”, so I need to figure out the best way to name it.

If that doesn’t scare you away, and you manage to download the code anyway, you can generate truth tables using the truth utility (in the bin directory):

$ ruby -Ilib bin/truth "(A || B) && C"
Expression:
(A || B) && C

 A  B  C | =
---------+---
 f  f  f | f 
 t  f  f | f 
 f  t  f | f 
 t  t  f | f 
 f  f  t | f 
 t  f  t | t 
 f  t  t | t 
 t  t  t | t 
 

Or, you can do so programmatically:

require 'truth'

table = Truth::Table.new "(A || B) && C"
table.display

Next week’s post will hopefully be more interesting. In the meantime, give this a try and let me know what you think.

The truth shall make you free!