You can split into pieces, convert shapes to Rationals , summarize Rationals, and convert the result to Float:
s = '1 1/2' f = s.split.map { |r| Rational(r) }.inject(:+).to_f
If you know that a string will always contain two parts, you can process the fragments separately:
s = '1 1/2' a = s.split f = a.first.to_i + Rational(a.last).to_f
If you are not sure how many parts will be (i.e. '1' , '3/2' , '11 23/42' , ... anything is possible), then the first should work in all cases.
Kernel # Rational will raise an ArgumentError if it cannot parse the string so you can wrap it all in begin / except to fix the errors.
source share