Code Snippet – Ruby Division Operations

Know Your Ruby Division Operations • Integer floor division 3/2 # = 1 • Floating point 3.fdiv(2) # = 1.5 • Permissive floating point • .to_f / 2 # = 1.5 nil.to_f / 2 # = 0.0 # Precise result = 3.quo(2) # = (3/2) result.class # => Rational Pick the right operation for the task at hand!

Responses