Month: April 2008

Rails: I can put together a simple database-backed app really quickly

Comment left here

… and that’s why I use RoR. 90% of the “anti” opinions here don’t get it. It’s quick, it has a low entry barrier, and, if you want the Ajax stuff, it’s very easy to do simple things. If you want stuff that scales to the moon then you use a technology that will do that – d’oh!

Ruby has also got lots of nice utilities to do SOAP and so on. It just works and it does’t hurt. Integration with other services (.Net or Java) is a breeze and you can get help really easily from other people in the community. In general there is only one way to do something (because the language is young) and it does what you want. Compare with Java – 25 frameworks with alpine learning curves (XML or properties files – let’s have both!) before you can do anything. It used to be simple and pretty easy to get things done but the signal to noise ratio is really painful now and getting worse with every “improved” J2EE implementation from the big vendors. Me no want no stinki’ entity beans – they leave a stain on your teeth.

I am a Java certified web developer and would’t go back, except for the cash. It used to drive me crazy: make a change, run Ant, deploy the WAR file, waste 20 minutes doing nothing. Get shouted at by PHB for reading El Reg while all this was going on. This is typical if you work on a legacy system.

Now I can just get stuff done, and that’s all I want. I’m really keen on the whole JRuby thing, where the Rails app will just run from a WAR file and use all the sexy scalability stuff that the Java people have spent so much time and energy on – but I’ll happily use it at one remove, thanks.

Getting the number of months between two dates in Ruby/Rails – updated

Here lies the one that works, not the old one I posted ages ago. It also uses dates instead of timestamp differences and should therefore not break if you ask it to go before the Epoch (some time in 1970 I think):

Adrie’s comment left on my old blog pointed this out (he’s much cleverer than me!) – it’s really trivial:

s is the start date and e is the end date (s is lower than e)

(e.month - s.month) + 12 * (e.year - s.year)

See!

module DateUtils

  class << self
    def months_between(date1 = Time.now,date2 = Time.now)

      date1 ||= Time.now
      date2 ||= Time.now

      if date1 < date2

        recent_date = date1.to_date
        past_date = date2.to_date

      else

        recent_date = date2.to_date
        past_date = date1.to_date

      end

      (past_date.month - recent_date.month) + 12 * (recent_date.year - past_date.year)

    end

  end

end

Imported Comments:

UE

Thanks a lot. helped me much

john

not working

fdsa

doesnt this neglect the actual day of the month? there isn’t 1 month between 2/1/11 and 1/31/11, but this would claim there is…

Francis

ok – but “months or part therof” is a long method name 🙂