Class: FractionTree::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#denominatorObject

Returns the value of attribute denominator.



218
219
220
# File 'lib/fraction_tree.rb', line 218

def denominator
  @denominator
end

#numeratorObject

Returns the value of attribute numerator.



218
219
220
# File 'lib/fraction_tree.rb', line 218

def numerator
  @numerator
end

#weightObject 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

Returns:

  • (Boolean)


244
245
246
# File 'lib/fraction_tree.rb', line 244

def eql?(rhs)
  rhs.kind_of?(self.class) && weight == rhs.weight
end

#inspectObject Also known as: to_s



228
229
230
# File 'lib/fraction_tree.rb', line 228

def inspect
  "(#{numerator}/#{denominator})"
end