Class: Fragment
Overview
An expression fragment. An expression can consist of many fragments.
Instance Attribute Summary collapse
-
#lv ⇒ Object
Returns the value of attribute lv.
-
#operand ⇒ Object
Returns the value of attribute operand.
Instance Method Summary collapse
- #*(value) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #evaluate ⇒ Object
-
#initialize(lv, operand) ⇒ Fragment
constructor
A new instance of Fragment.
- #to_s ⇒ Object
- #variable ⇒ Object
Constructor Details
#initialize(lv, operand) ⇒ Fragment
Returns a new instance of Fragment.
64 65 66 67 |
# File 'lib/rulp/expression.rb', line 64 def initialize(lv, operand) @lv = lv @operand = operand end |
Instance Attribute Details
#lv ⇒ Object
Returns the value of attribute lv.
62 63 64 |
# File 'lib/rulp/expression.rb', line 62 def lv @lv end |
#operand ⇒ Object
Returns the value of attribute operand.
62 63 64 |
# File 'lib/rulp/expression.rb', line 62 def operand @operand end |
Instance Method Details
#*(value) ⇒ Object
77 78 79 |
# File 'lib/rulp/expression.rb', line 77 def *(value) Fragment.new(@lv, @operand * value) end |
#+(other) ⇒ Object
69 70 71 |
# File 'lib/rulp/expression.rb', line 69 def +(other) return Expressions.new([self] + Expressions[other].expressions) end |
#-(other) ⇒ Object
73 74 75 |
# File 'lib/rulp/expression.rb', line 73 def -(other) self.+(-other) end |
#-@ ⇒ Object
89 90 91 92 |
# File 'lib/rulp/expression.rb', line 89 def -@ @operand = -@operand self end |
#evaluate ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/rulp/expression.rb', line 81 def evaluate if [TrueClass,FalseClass].include? @lv.value.class @operand * (@lv.value ? 1 : 0) else @operand * @lv.value end end |
#to_s ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rulp/expression.rb', line 104 def to_s @as_str ||= begin case @operand when -1 " - #{@lv}" when 1 " + #{@lv}" else " + #{@operand} #{@lv}" end end end |
#variable ⇒ Object
94 95 96 |
# File 'lib/rulp/expression.rb', line 94 def variable @lv end |