Class: Ilp::Term
- Inherits:
-
Object
- Object
- Ilp::Term
- Defined in:
- lib/ruby-cbc/ilp/term.rb
Instance Attribute Summary collapse
-
#mult ⇒ Object
Returns the value of attribute mult.
-
#var ⇒ Object
readonly
Returns the value of attribute var.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=(other) ⇒ Object
- #==(other) ⇒ Object
- #>=(other) ⇒ Object
- #coerce(num) ⇒ Object
- #combine_in(other_term) ⇒ Object
-
#initialize(var, mult = 1) ⇒ Term
constructor
A new instance of Term.
- #to_s ⇒ Object
Constructor Details
#initialize(var, mult = 1) ⇒ Term
Returns a new instance of Term.
6 7 8 9 |
# File 'lib/ruby-cbc/ilp/term.rb', line 6 def initialize(var, mult = 1) @mult = mult @var = var end |
Instance Attribute Details
#mult ⇒ Object
Returns the value of attribute mult.
4 5 6 |
# File 'lib/ruby-cbc/ilp/term.rb', line 4 def mult @mult end |
#var ⇒ Object (readonly)
Returns the value of attribute var.
3 4 5 |
# File 'lib/ruby-cbc/ilp/term.rb', line 3 def var @var end |
Instance Method Details
#*(other) ⇒ Object
38 39 40 41 |
# File 'lib/ruby-cbc/ilp/term.rb', line 38 def *(other) raise ArgumentError, "Argument is not numeric" unless other.is_a? Numeric Ilp::Term.new(@var, @mult * other) end |
#+(other) ⇒ Object
11 12 13 |
# File 'lib/ruby-cbc/ilp/term.rb', line 11 def +(other) Ilp::TermArray.new([self]) + other end |
#-(other) ⇒ Object
15 16 17 |
# File 'lib/ruby-cbc/ilp/term.rb', line 15 def -(other) Ilp::TermArray.new([self]) - other end |
#<=(other) ⇒ Object
23 24 25 |
# File 'lib/ruby-cbc/ilp/term.rb', line 23 def <=(other) Ilp::TermArray.new([self]) <= other end |
#==(other) ⇒ Object
19 20 21 |
# File 'lib/ruby-cbc/ilp/term.rb', line 19 def ==(other) Ilp::TermArray.new([self]) == other end |
#>=(other) ⇒ Object
27 28 29 |
# File 'lib/ruby-cbc/ilp/term.rb', line 27 def >=(other) Ilp::TermArray.new([self]) >= other end |
#coerce(num) ⇒ Object
43 44 45 |
# File 'lib/ruby-cbc/ilp/term.rb', line 43 def coerce(num) [Ilp::TermArray.new([self]), num] end |
#combine_in(other_term) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/ruby-cbc/ilp/term.rb', line 31 def combine_in(other_term) return Term.new(var, mult) unless other_term raise "Terms cannot be combined: #{self} and #{other_term}" unless var.equal? other_term.var other_term.mult += mult other_term end |
#to_s ⇒ Object
47 48 49 50 51 |
# File 'lib/ruby-cbc/ilp/term.rb', line 47 def to_s str = "++-"[mult <=> 0] << " " str << mult.abs.to_s << " " if mult != 1 str << var.to_s end |