Sha256: 4a79f0f6cc529c9540f020028af806df44ee9a0fd3134f8a6e8980310c1a66fb
Contents?: true
Size: 1.48 KB
Versions: 1
Compression:
Stored size: 1.48 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.keys.include?(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.0 | lib/logicle/node.rb |