Class: Nelson::Term
Instance Attribute Summary collapse
-
#raw_value ⇒ Object
readonly
Returns the value of attribute raw_value.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
- #coerce(other) ⇒ Object
-
#initialize(value) ⇒ Term
constructor
A new instance of Term.
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_r ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value) ⇒ Term
Returns a new instance of Term.
7 8 9 10 |
# File 'lib/nelson/term.rb', line 7 def initialize(value) @raw_value = value @value = value.is_a?(Numeric) ? value.to_r : value end |
Instance Attribute Details
#raw_value ⇒ Object (readonly)
Returns the value of attribute raw_value.
5 6 7 |
# File 'lib/nelson/term.rb', line 5 def raw_value @raw_value end |
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/nelson/term.rb', line 5 def value @value end |
Instance Method Details
#*(other) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/nelson/term.rb', line 53 def *(other) if other.is_a? Term Term.new(value * other.value) elsif other.is_a? Numeric Term.new(value * other) else if other.respond_to? :coerce a, b = other.coerce(self) a * b else raise TypeError, "#{other.class} can't be coerced into Term" end end end |
#+(other) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/nelson/term.rb', line 21 def +(other) if other.is_a? Term Term.new(value + other.value) elsif other.is_a? Numeric Term.new(value + other) elsif other.is_a? Expression Term.new(value + other.call) else if other.respond_to? :coerce a, b = other.coerce(self) a + b else raise TypeError, "#{other.class} can't be coerced into Term" end end end |
#-(other) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/nelson/term.rb', line 38 def -(other) if other.is_a? Term Term.new(value - other.value) elsif other.is_a? Numeric Term.new(value - other) else if other.respond_to? :coerce a, b = other.coerce(self) a - b else raise TypeError, "#{other.class} can't be coerced into Term" end end end |
#/(other) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nelson/term.rb', line 68 def /(other) if other.is_a? Term Term.new(value / other.value) elsif other.is_a? Numeric Term.new(value / other) else if other.respond_to? :coerce a, b = other.coerce(self) a / b else raise TypeError, "#{other.class} can't be coerced into Term" end end end |
#<=>(other) ⇒ Object
12 13 14 |
# File 'lib/nelson/term.rb', line 12 def <=>(other) other <=> @value end |
#coerce(other) ⇒ Object
83 84 85 |
# File 'lib/nelson/term.rb', line 83 def coerce(other) [Term.new(other), self] end |
#to_f ⇒ Object
95 96 97 |
# File 'lib/nelson/term.rb', line 95 def to_f value.to_f end |
#to_i ⇒ Object
99 100 101 |
# File 'lib/nelson/term.rb', line 99 def to_i value.to_i end |
#to_r ⇒ Object
91 92 93 |
# File 'lib/nelson/term.rb', line 91 def to_r value end |
#to_s ⇒ Object
87 88 89 |
# File 'lib/nelson/term.rb', line 87 def to_s raw_value.to_s end |