Class: MathViz::Operation::Binary
- Inherits:
-
MathViz::Operation
- Object
- Term
- MathViz::Operation
- MathViz::Operation::Binary
- Defined in:
- lib/mathviz.rb
Overview
Display and processing for two-value operators
Instance Attribute Summary
Attributes inherited from Term
Instance Method Summary collapse
-
#color ⇒ Object
Graphviz node color; differentiates basic mathematical operators (+, -, *, /).
-
#constant? ⇒ Boolean
True only if both operands are #constant?.
-
#initialize(a, op, b) ⇒ Binary
constructor
A new instance of Binary.
-
#long ⇒ Object
Debugging method; returns string of names and values.
-
#shape ⇒ Object
Graphviz node shape; differentiates comparison operators.
-
#to_dot(g) ⇒ Object
Extend Graphviz g with a representation of this object, and incoming connections.
-
#to_f ⇒ Object
Apply the operator to create the derived value.
-
#units ⇒ Object
Apply the operator to create the derived units.
Methods inherited from MathViz::Operation
Methods inherited from Term
binop, #data, #label, list_terms, name_terms!, #style, #to_i, #to_s, unop
Methods included from Measured
Methods included from Units::Class
Constructor Details
#initialize(a, op, b) ⇒ Binary
Returns a new instance of Binary.
537 538 539 540 541 542 |
# File 'lib/mathviz.rb', line 537 def initialize(a, op, b) super() @a = term(a) @op = op @b = term(b) end |
Instance Method Details
#color ⇒ Object
Graphviz node color; differentiates basic mathematical operators (+, -, *, /)
560 561 562 563 564 565 566 567 568 |
# File 'lib/mathviz.rb', line 560 def color case @op when :+; :green; when :-; :yellow; when :*; :blue; when :/; :cyan; else :red; end end |
#constant? ⇒ Boolean
True only if both operands are #constant?
590 591 592 |
# File 'lib/mathviz.rb', line 590 def constant? @a.constant? && @b.constant? end |
#long ⇒ Object
Debugging method; returns string of names and values
545 546 547 548 |
# File 'lib/mathviz.rb', line 545 def long n = @name && (@name + " = ") "(#{n}#{@a} #{@op} #{@b} = #{to_f})" end |
#shape ⇒ Object
Graphviz node shape; differentiates comparison operators
551 552 553 554 555 556 557 |
# File 'lib/mathviz.rb', line 551 def shape if ([:>, :<, :>=, :<=, :&, :|, :==].include? @op) :ellipse else :box end end |
#to_dot(g) ⇒ Object
Extend Graphviz g with a representation of this object, and incoming connections
571 572 573 574 575 576 577 |
# File 'lib/mathviz.rb', line 571 def to_dot(g) super (g[@a.node] >> g[node]) [:arrowhead => :normal, :headlabel => @op.to_s, :labeldistance => '2'] (g[@b.node] >> g[node]) [:arrowhead => :onormal] @a.to_dot(g) if (@a.respond_to?(:name) && @a.name.nil?) @b.to_dot(g) if (@b.respond_to?(:name) && @b.name.nil?) end |
#to_f ⇒ Object
Apply the operator to create the derived value.
580 581 582 |
# File 'lib/mathviz.rb', line 580 def to_f @a.to_f.__send__(@op, @b.to_f) end |
#units ⇒ Object
Apply the operator to create the derived units.
585 586 587 |
# File 'lib/mathviz.rb', line 585 def units @a.units.__send__(@op, @b.units) end |