Module: StepSequencer::Refinements::StringRationalEval
- Defined in:
- lib/step_sequencer/refinements.rb
Overview
String#rational_eval
a method I’ve written to help work with rational numbers. It evals a string containing math, but wraps all number values in a call It raises an error if the result is not a rational
Instance Method Summary collapse
Instance Method Details
#rational_eval ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/step_sequencer/refinements.rb', line 58 def rational_eval result = eval <<-RB Rational(#{ gsub(/\d[\d\.\_]*/) { |str| "Rational(#{str})"} }) RB result.is_a?(Rational) ? result : raise("#{result} is not Rational") end |