Name: Anonymous 2012-01-04 21:50
class TrueClass
def implies(x) x end
def nand(x) not x end
def xnor(x) x end
def xor(x) not x end
def nor(x) false end
def not(x) !x end
end
class FalseClass
def implies(x) true end
def nand(x) x end
def xnor(x) not x end
def xor(x) x end
def nor(x) not x end
def not(x) x end
end
module Logic
class TruthTableRow
attr_accessor :cell
def initialize(p, q, message)
@cell = [p, q, p.send(message, q)]
end
end
class TruthTable
attr_accessor :rows, :message
def initialize(message)
@rows = [TruthTableRow.new(true, true, message),
TruthTableRow.new(true, false, message),
TruthTableRow.new(false, true, message),
TruthTableRow.new(false, false, message)]
@message = message
end
def printTable
rows.each do |a|
print "#{a.cell[0]} #{lambda{if a.cell[0] then " " else "" end}.call}#{message.to_s} #{a.cell[1]} #{lambda{if a.cell[1] then " " else "" end}.call}: #{a.cell[2]}\n"
end
end
end
KeywordList = ["and", "or", "nand", "nor" "xor", "xnor", "implies", "not"]
def Logic.mainProgram(x)
if KeywordList.include?(x) then
print "\n\tP #{x.upcase} Q\n"
TruthTable.new(x.to_sym).printTable
else
print "#{x.capitalize} is not a logic gate\n"
end
end
end
if ARGV.length >= 1 then
Logic::mainProgram(ARGV[0].to_s.downcase)
else
print "Wrong number of arguments\n"
endMade this lovely piece of OOP in a functional programming class... which was more of a logic class because we haven't seen a single line of code yet this quarter.