My girlfriend asked me about the difference between to_f (to float) and to_d (to decimal) in Ruby. By the way, the method to_d is a support method from the ruby library “bigdecimal/util”
I figured that the bigdecimal library provides a more precise decimal number than floats. But it was not all.
After some exploration she explained to me that while it is true that to_d is more precise the main difference from to_f is that it does not work with nil.
require 'bigdecimal' require 'bigdecimal/util' nil = Nil.new nil.to_f #=> 0.0 nil.to_d # => NoMethodError: undefined method `to_d' for nil:NilClass 12.to_d # 12.0
She continues explaining to me that this main reason why she needs to use float not decimal in her code (for example for when a user has not provided a number).
I love learning from her 🙂