Class: FractionTree::Node
- Inherits:
-
Object
- Object
- FractionTree::Node
- Includes:
- Comparable
- Defined in:
- lib/fraction_tree.rb
Instance Attribute Summary collapse
-
#denominator ⇒ Object
Returns the value of attribute denominator.
-
#numerator ⇒ Object
Returns the value of attribute numerator.
-
#weight ⇒ Object
(also: #to_r)
Returns the value of attribute weight.
Instance Method Summary collapse
- #+(rhs) ⇒ Object
- #<=>(rhs) ⇒ Object
- #==(rhs) ⇒ Object
-
#eql?(rhs) ⇒ Boolean
Needed for intersection operations to work.
-
#initialize(n, d) ⇒ Node
constructor
A new instance of Node.
- #inspect ⇒ Object (also: #to_s)
Constructor Details
#initialize(n, d) ⇒ Node
Returns a new instance of Node.
220 221 222 223 224 |
# File 'lib/fraction_tree.rb', line 220 def initialize(n,d) @numerator = n @denominator = d @weight = (d == 0 ? Float::INFINITY : Rational(@numerator, @denominator)) end |
Instance Attribute Details
#denominator ⇒ Object
Returns the value of attribute denominator.
218 219 220 |
# File 'lib/fraction_tree.rb', line 218 def denominator @denominator end |
#numerator ⇒ Object
Returns the value of attribute numerator.
218 219 220 |
# File 'lib/fraction_tree.rb', line 218 def numerator @numerator end |
#weight ⇒ Object Also known as: to_r
Returns the value of attribute weight.
218 219 220 |
# File 'lib/fraction_tree.rb', line 218 def weight @weight end |
Instance Method Details
#+(rhs) ⇒ Object
248 249 250 |
# File 'lib/fraction_tree.rb', line 248 def +(rhs) self.class.new(self.numerator+rhs.numerator, self.denominator+rhs.denominator) end |
#<=>(rhs) ⇒ Object
233 234 235 |
# File 'lib/fraction_tree.rb', line 233 def <=>(rhs) self.weight <=> rhs.weight end |
#==(rhs) ⇒ Object
237 238 239 |
# File 'lib/fraction_tree.rb', line 237 def ==(rhs) self.weight == rhs.weight end |
#eql?(rhs) ⇒ Boolean
Needed for intersection operations to work. blog.mnishiguchi.com/ruby-intersection-of-object-arrays shortrecipes.blogspot.com/2006/10/ruby-intersection-of-two-arrays-of.html
244 245 246 |
# File 'lib/fraction_tree.rb', line 244 def eql?(rhs) rhs.kind_of?(self.class) && weight == rhs.weight end |
#inspect ⇒ Object Also known as: to_s
228 229 230 |
# File 'lib/fraction_tree.rb', line 228 def inspect "(#{numerator}/#{denominator})" end |