Class: Logicle::Node
- Inherits:
-
Object
- Object
- Logicle::Node
- Defined in:
- lib/logicle/node.rb
Constant Summary collapse
- 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) } }
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #append_inputs(*nodes) ⇒ Object (also: #append_input)
- #bulb? ⇒ Boolean
- #clear_inputs ⇒ Object
-
#initialize(id, type) ⇒ Node
constructor
A new instance of Node.
- #state ⇒ Object
- #switch? ⇒ Boolean
Constructor Details
#initialize(id, type) ⇒ Node
Returns a new instance of Node.
19 20 21 22 23 |
# File 'lib/logicle/node.rb', line 19 def initialize(id, type) @id = id @type = validate_type(type) @inputs = [] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
17 18 19 |
# File 'lib/logicle/node.rb', line 17 def id @id end |
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
17 18 19 |
# File 'lib/logicle/node.rb', line 17 def inputs @inputs end |
#type ⇒ Object
Returns the value of attribute type.
17 18 19 |
# File 'lib/logicle/node.rb', line 17 def type @type end |
Instance Method Details
#append_inputs(*nodes) ⇒ Object Also known as: append_input
37 38 39 |
# File 'lib/logicle/node.rb', line 37 def append_inputs(*nodes) @inputs += nodes end |
#bulb? ⇒ Boolean
25 26 27 |
# File 'lib/logicle/node.rb', line 25 def bulb? @type == :bulb end |
#clear_inputs ⇒ Object
42 43 44 45 |
# File 'lib/logicle/node.rb', line 42 def clear_inputs @inputs = [] @state = nil end |
#state ⇒ Object
47 48 49 |
# File 'lib/logicle/node.rb', line 47 def state @state ||= evaluate end |
#switch? ⇒ Boolean
29 30 31 |
# File 'lib/logicle/node.rb', line 29 def switch? @type == :switch end |