Sha256: c1b054a3b82b622ae1358e23df83c7f5474f5dd14cb1964edb1b3af377fc120b
Contents?: true
Size: 1.47 KB
Versions: 1
Compression:
Stored size: 1.47 KB
Contents
module Logicle class Node LOGIC_OPERATIONS = { switch: Proc.new { |args| nil }, bulb: Proc.new { |args| args[0].state }, on: Proc.new { |args| true }, off: Proc.new { |args| false }, not: Proc.new { |args| !args[0].state }, and: Proc.new { |args| args[0].state & args[1].state }, or: Proc.new { |args| args[0].state | args[1].state }, nand: Proc.new { |args| !args[0].state | !args[1].state }, nor: Proc.new { |args| !args[0].state & !args[1].state }, xor: Proc.new { |args| args[0].state ^ args[1].state }, xnor: Proc.new { |args| !(args[0].state ^ args[1].state) } } attr_reader :id, :type, :inputs def initialize(id, type) @id = id @type = validate_type(type) @inputs = [] end def bulb? @type == :bulb end def switch? @type == :switch end def type=(new_type) @type = validate_type(new_type) end def append_inputs(*nodes) @inputs += nodes end alias_method :append_input, :append_inputs def clear_inputs @inputs = [] @state = nil end def state @state ||= evaluate end private def validate_type(type) if LOGIC_OPERATIONS.has_key?(type) type else raise UnknownNodeTypeError, "Unknown node type: '#{ type }'" end end def evaluate operation = LOGIC_OPERATIONS[@type] operation.call(@inputs) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
logicle-0.1.1 | lib/logicle/node.rb |