Add two rational number up : Rational Numbers « Number « Ruby

Add two rational number up

#!/usr/bin/env ruby

require 'rational'
require 'mathn'

rat = Rational(25/100) # => 1/4 -- lowest terms

rat + 1/4 # => 1/2


Related examples in the same category

1.Rational Numbers to create a fraction;
3.Create a rational number
4.Subtract one number from another
5.Rational number without using Rational class
6.Multiply two rational numbers
7.Divide two rational numbers to add (+), subtract (-), multiply (*), and divide (/) fractions; to perform modulo (%), power (**), and equality (== or <=>); to produce a string or a float representation of a fraction (inspect).
11.Compare two rational numbers
12.Comparable operator on rational numbers
13.Inspect a rational number
14.Power a rational number
15.Modulo two rational numbers