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.
63 64 65 66 |
# File 'lib/rulp/expression.rb', line 63 def initialize(lv, operand) @lv = lv @operand = operand end |
Instance Attribute Details
#lv ⇒ Object
Returns the value of attribute lv.
61 62 63 |
# File 'lib/rulp/expression.rb', line 61 def lv @lv end |
#operand ⇒ Object
Returns the value of attribute operand.
61 62 63 |
# File 'lib/rulp/expression.rb', line 61 def operand @operand end |
Instance Method Details
#*(value) ⇒ Object
76 77 78 |
# File 'lib/rulp/expression.rb', line 76 def *(value) Fragment.new(@lv, @operand * value) end |
#+(other) ⇒ Object
68 69 70 |
# File 'lib/rulp/expression.rb', line 68 def +(other) return Expressions.new([self] + Expressions[other].expressions) end |
#-(other) ⇒ Object
72 73 74 |
# File 'lib/rulp/expression.rb', line 72 def -(other) self.+(-other) end |
#-@ ⇒ Object
88 89 90 91 |
# File 'lib/rulp/expression.rb', line 88 def -@ @operand = -@operand self end |
#evaluate ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/rulp/expression.rb', line 80 def evaluate if [TrueClass,FalseClass].include? @lv.value.class @operand * (@lv.value ? 1 : 0) else @operand * @lv.value end end |
#to_s ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rulp/expression.rb', line 103 def to_s @as_str ||= begin case @operand when -1 then " - #{@lv}" when 1 then " + #{@lv}" when ->(op){ op < 0} then " - #{@operand.abs} #{@lv}" else " + #{@operand} #{@lv}" end end end |
#variable ⇒ Object
93 94 95 |
# File 'lib/rulp/expression.rb', line 93 def variable @lv end |