Class: Nelson::Term

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/nelson/term.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_valueObject (readonly)

Returns the value of attribute raw_value.



5
6
7
# File 'lib/nelson/term.rb', line 5

def raw_value
  @raw_value
end

#valueObject

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_fObject



95
96
97
# File 'lib/nelson/term.rb', line 95

def to_f
  value.to_f
end

#to_iObject



99
100
101
# File 'lib/nelson/term.rb', line 99

def to_i
  value.to_i
end

#to_rObject



91
92
93
# File 'lib/nelson/term.rb', line 91

def to_r
  value
end

#to_sObject



87
88
89
# File 'lib/nelson/term.rb', line 87

def to_s
  raw_value.to_s
end