How Many Days Ago in Ruby
So Weiqi wondered How Many Days Ago was that. Then Eric posted a Java solution.
Since I have been attempting to learn Ruby, I figured I would attempt to implement a solution in Ruby. I would like to preface this post by stating that I am a Ruby novice and I'm sure there is a better way to do this. Here's the "How Many Days Ago Was That" in Ruby.
Since I have been attempting to learn Ruby, I figured I would attempt to implement a solution in Ruby. I would like to preface this post by stating that I am a Ruby novice and I'm sure there is a better way to do this. Here's the "How Many Days Ago Was That" in Ruby.
require "parsedate"
require "date"
def usage()
puts "Usage: daysago.rb date"
exit 1
end
def computeDaysBetween(from, to)
from, to = to, from if (from > to) # Swaps the two variables
daysAgo = 0
from.upto(to) {|day|daysAgo+=1}
return daysAgo
end
from = Date.parse(ARGV.shift || usage())
puts "It has been #{computeDaysBetween(from, Date.today)} since #{from}"

0 Comments:
Post a Comment
<< Home