It is possible to use formatted string output
Examples:
puts sprintf('%02i', 8)
puts '%02i' % 8
%02iis a format for two digits (an integer) with leading zeros. Details can be found in the documentation for sprintf
In your specific case with the date, you can simply use the Time#strftimeod method Date#strftime:
require 'time'
puts Time.new(2015,8,1).strftime("%m")
source
share