Class: String

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

Instance Method Summary collapse

Instance Method Details

#to_nodeFractionTree::Node

Returns string Stern-Brocot decoded.

Examples:

"1/0".to_node => (1/0)

Returns:



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fraction_tree/extensions.rb', line 6

def to_node
  if self.include?(".")
    number = self.to_d
    numerator, denominator = number.numerator, number.denominator
  elsif self.include?("/")
    (numerator, denominator) = self.split("/").map(&:to_i)
  else
    number = self.to_r
    numerator, denominator = number.numerator, number.denominator
  end
  number = denominator.zero? ? Float::INFINITY : Rational(numerator, denominator)
  FractionTree::Node.new(number)
end