Monday, December 13, 2004

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.

require "parsedate"
require "date"

def usage()
puts "Usage: daysago.rb date"
exit 1

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

from = Date.parse(ARGV.shift || usage())
puts "It has been #{computeDaysBetween(from,} since #{from}"


Post a Comment

<< Home