Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This sounds like a good situation for the observer pattern. This is a sample of that in ruby:</p> <pre><code>require 'observer' class Node attr_accessor :id @@current_node_id = 0 def initialize @@current_node_id += 1 id = @@current_node_id end include Observable attr_reader :value protected def value=(new_value) return if @value == new_value old_value = @value @value = new_value changed notify_observers(id, old_value, @value) end end class ValueNode &lt; Node def initialize(initial_value) super() @value = initial_value end def value=(new_value) super(new_value) end end class SumNode &lt; Node def initialize(*nodes) super() @value = nodes.map(&amp;:value).inject(0, &amp;:+) nodes.each do |node| node.add_observer(self) end end def update(id, old_value, new_value) self.value = self.value - old_value + new_value end end def test v1 = ValueNode.new 4 v2 = ValueNode.new 8 sum = SumNode.new(v1, v2) sum2 = SumNode.new(v1, sum) v2.value = 10 p sum.value p sum2.value end test() </code></pre> <p>Notice how the value of <code>SumNode</code> isn't recalculated every time it is requested - instead it is updated when one of its value nodes is updated. This works recursively, so that inner <code>SumNodes</code> also trigger updates. As the notification includes the unique <code>id</code> of the node, it is possible to write more complex <code>Node</code> types, such as ones that contain formulas. </p> <p>See <a href="http://www.ruby-doc.org/stdlib/libdoc/observer/rdoc/index.html" rel="nofollow">http://www.ruby-doc.org/stdlib/libdoc/observer/rdoc/index.html</a> for more details on Observable</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload