Skip to content

Instantly share code, notes, and snippets.

@DVG
Created March 1, 2013 13:47
Show Gist options
  • Save DVG/5064760 to your computer and use it in GitHub Desktop.
Save DVG/5064760 to your computer and use it in GitHub Desktop.
Prints out a boolean truth table for a given number of variables
class Combinations
attr_accessor :number_of_variables
def initialize(number_of_variables=2)
@number_of_variables = number_of_variables
end
def print_combinations
combinations.each do |row|
row.each_with_index do |v, i|
print "| " if i == 0
print string_val(v)
print " | " unless i == number_of_variables
print "\n" if i == number_of_variables - 1
end
end
end
def combinations
([true, false]*number_of_variables).combination(number_of_variables).to_a.uniq
end
def string_val(val)
if val == true
"T"
else
"F"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment