Class: HpSqrt::Term
- Inherits:
-
Object
- Object
- HpSqrt::Term
- Defined in:
- lib/hpsqrt/term.rb
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#sqrt ⇒ Object
readonly
Returns the value of attribute sqrt.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(number: 1, sqrt: 1) ⇒ Term
constructor
A new instance of Term.
Constructor Details
#initialize(number: 1, sqrt: 1) ⇒ Term
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/hpsqrt/term.rb', line 7 def initialize(number: 1, sqrt: 1) unless Numeric===number raise TypeError, "can't convert %s into %s: %s" % [number.class.name, self.class.name, number.inspect] end unless Numeric===sqrt raise TypeError, "can't convert %s into %s: %s" % [sqrt.class.name, self.class.name, sqrt.inspect] end @number = number @sqrt = sqrt freeze end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
4 5 6 |
# File 'lib/hpsqrt/term.rb', line 4 def number @number end |
#sqrt ⇒ Object (readonly)
Returns the value of attribute sqrt.
5 6 7 |
# File 'lib/hpsqrt/term.rb', line 5 def sqrt @sqrt end |
Instance Method Details
#*(other) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/hpsqrt/term.rb', line 20 def *(other) if self.class===other n = @number * other.number s = 1 if @sqrt==other.sqrt n *= @sqrt else s = @sqrt * other.sqrt end self.class.new(number: n, sqrt: s) end end |
#eql?(other) ⇒ Boolean
34 35 36 |
# File 'lib/hpsqrt/term.rb', line 34 def eql?(other) self.class===other && @number==other.number && @sqrt==other.sqrt end |
#hash ⇒ Object
38 39 40 |
# File 'lib/hpsqrt/term.rb', line 38 def hash [@number, @sqrt].hash end |