Add weekdays in Ruby on Rails

Posted by – March 9, 2009

I am working on an application where I need to be able to add 3 week days to today’s date. Unfortunately there doesn’t seem to be any built-in functionality for this in Ruby, so I needed to roll my own. At first I searched Google hoping someone had already solved the problem for me, which is most often the case. Not so with this problem. I did however come across this post on Platte daddy, which provided a good starting point, but was designed to simply add 1 weekday, not any number of days that I want.

I ran through a number of iterations until I came up with this helper method:


def add_cwdays(date, n)
#Add number of weekdays plus necessary weekend days
date += n.days + (2*(n/5.floor)).days
#If the above lands on a weekend, keep adding days until it is a weekday
date += 1.days until (1..5).member?(date.wday)
date
end

This was added to my ApplicationHelper.rb file and is called for adding 3 week days to today via:


add_cwdays(Time.now, 3)

I created a helper to DRY the method and I wrote it so that the starting date and the number of workdays is variable. The helper has only 2 lines of code (after some re-factoring), which reduces the readability a bit, but it still makes sense I think.

The first line simply adds the desired number of weekdays, plus any weekend days that would be within the range of week days. If we wanted to add 8 weekdays, we would need to add 10 total calendar days (1 weekend). If we wanted to add 23 weekdays, we would need to add 31 total calendar days (4 weekends). If we wanted to add 4 weekdays, we would need to add 4 total calendar days (no weekends). The weekend addition is made with the (2*(n/5.floor)).days bit of code. Basically take the number of days “n”, divide by five, strip off the decimal and multiply by 2 (for Saturday and Sunday).

The second line of code deals with the eventuality of the first line landing on a weekend. If this happens, we simply add 1 day repeatedly “until” we reach a weekday.

This seems to be working well, if anyone looks this over and sees anything wrong or if there are any opportunities to re-factor and simplify, please post comments.

Make Safari’s windows behave properly

Posted by – March 4, 2009

via: MacLife

I have not used Safari as my primary browser in OSX primarily because I prefer the tab behaviour of Firefox. This was true until I found this interesting article (See #4) on MacLife about changing the way Safari handles tabs.

Simply open up the Terminal and enter the following:

defaults write com.apple.Safari TargetedClicksCreateTabs -bool TRUE

This little gem tells Safari to open new windows in new tabs instead of in an entirely new window, just like Firefox does. Hopefully Apple will make this simpler in the future via an option in Safari’s Preferences, but until then I think this will suffice.

Now all I need Apple to do is to get auto-complete of URLs working like Firefox…

USF1

Posted by – February 24, 2009

via: Autoblog

USF1 has officially announced plans to compete in the 2010 Formula One series. There has been a bit of speculation throughout the interwebs of late regarding this, but nothing official until now. The team will be based in Charlotte, NC and they plan to operate more like a skunk-works than a traditional F1 team. Seems reminiscent of Super Aguri, which saw limited success with a tiny (comparitively) budget.

Of particular note is their intention to be open and accesible to their fans. Traditionally, F1 is a super-secret society, shielding all news and developments from prying eyes. It will be interesting to see just how open they will be.

Google Sync

Posted by – February 9, 2009

Google has added Sync! What does this mean, well if you own an iPhone or other compatible device and you have a Gmail account you can now wirelessly and automagically sync your contacts and calendar with your Gmail contacts and Google Calendar. This is a really nice feature which makes the iPhone + Google combination every bit as capable as the BlackBerry and Microsoft Exchange combo. With this latest release from Google, an enterprise is no longer tied to Microsoft Exchange and RIMs Blackberry for enterprise class mobility.

A telling little change?

Posted by – January 20, 2009

via: Digg

Kottke.org has an article “The country’s new robots.txt file” that shows what will hopefully be the first of many, many changes to the way the President of the United States of America executes the duties of his office.

Basically, the Bush administration was blocking search engines from finding information on the official White House website (almost 2,400 individual blocking statements). As soon as the Obama administration changed the site, all of those restrictions have been removed. Hopefully by the end of Obama’s tenure he will not find it necessary to try so hard to hide information from the people he serves.

Linux server instructions

Posted by – January 20, 2009

Slicehost has some really nice tutorials and instructions for setting up a Linux server to get a website up and running. I just followed the tutorial for setting up a Ruby on Rails site being server by Apache using Passenger. If this sounds like a foreign language, well the names for software in the FOSS world can be a bit confusing but once involved in the process they become more familiar.

There are four basic steps that I followed:

1. Install and configure the server’s operating system (parts 1 & 2, Ubuntu 8.10 (aka Intrepid Ibex)
2. Install Ruby, Ruby Gems and Rails
3. Install Apache and Passenger and configure them to serve up a Rails site.
4. Make a website

Not too difficult, well, except for step #4! Ha! All told, the entire process takes about an hour (most of that spent downloading, installing and updating the software).

Flickr + iPhone = Sweet!

Posted by – December 30, 2008

I was just playing around with Flickr on my iPhone and noticed that you can upload images to your Flickr account via a unique email address that is specific to your account. With this new found knowledge I set about uploading a few pictures to test it out, and it works great. I then began to think that it would be nice to be able to tag the photos from the email (the subject line becomes the photo’s title), but how to do this wasn’t readily apparent. A quick search on Google led me to this handy bit of information from the Flickr Help:

Just type “tags:” in the subject line or body of the email followed by the list of tags you want added to the photo(s).

Doesn’t get much simpler than that! Here is a link to the set I created for pictures uploaded from my iPhone: http://www.flickr.com/photos/celerity/sets/72157611915278668/.

[UPDATE] Feature request – Allow separate email addresses for individual photo sets in my Flickr account.

[UPDATE] Apparently Apple added geocoding to the camera when they released the iPhone 2.0 software update, very nice…

omg ur bn robbed

Posted by – December 29, 2008

The New York Times has just posted an article about a particularly annoying pet peeve of mine. Since owning an iPhone I have had numerous arguments with people regarding the iPhone’s lack of MMS (picture text messages). These people ridicule the iPhone over it’s lack of this “feature”. The basic argument is thus: “My phone can send pictures in a text message, why can’t your shiny new iPhone?”. No matter that their phone can’t send or receive email, which is an exponentially better messaging platform than SMS or MMS. What everyone that has argued over this fails to understand is that they are being ripped off by this amazing and wonderful “feature”. The cell phone carriers are charging exorbitant amounts of money to send tiny, text based messages and blurry photos to and from cell phones exclusively (excluding the SMS/MMS to email gateways). I can send a photo attached to an email to a phone or computer. It can be viewed on any number of devices and it doesn’t cost me anything extra. SMS should be free, it doesn’t cost the cell phone carriers anything (relatively) to deliver the messages. MMS should go away and be replaced with email on all phones. Neither should be touted as a “feature”.

On a side note: I am actually a little upset that AT&T requires that I have a text messaging plan in my contract, I would prefer to never send or receive messages via text messaging as it is an old and over-priced technology. That I am forced to pay a flat rate for a text-messaging plan is ridiculous.

Cars I like

Posted by – December 8, 2008

Amidst all the turmoil in the US auto industry I have decided to compile a short list of cars that, if offered in the US by any of the Big 3 would sell well and perhaps reverse their current state of affairs. Common to all three of the cars in this list, small, economical, good quality, safe, fun to drive, not made by the Big 3. I don’t want a giant truck or a car designed by accountants and built for fleet sales.

Honda Civic (UK)

Honda Civic (UK)


1. Honda Civic 3 Door: Unfortunately, this vehicle is only available outside of the US. Apparently the major auto manufacturers don’t think that Americans want to buy sporty, economical and uber-convenient hatchbacks.

Subaru Imreza WRX

Subaru Imreza WRX


2. Subaru Impreza WRX: Unlike the Civic, this one is available in the US. Fortunately Subaru have seen the error of their ways in regards to the less than stellar performance of the 2008 model year and have made the 2009 WRX more like it’s predecessors. Only time will tell if it will maintain the iconic rally car status of years past. Unfortunately, the gas mileage is not all that impressive.

Audi A3

Audi A3


3. Audi A3: This is such a nice car! The only problem with this one is, of course, the price.

Secure your Google Mail

Posted by – November 10, 2008

Keeping your email secure is probably the single most important aspect of your online life. Every web-based service that you use, more than likely, will send allow you to reset your password using your email. Imagine if someone was able to access your email, they would be able to reset the passwords for your bank, cell phone, water bill, etc…

To help thwart would be attackers take a few moments to ensure that your Google Mail is secure (the same goes if you aren’t using Google Mail).

  • Google now has a feature that forces the use of HTTPS when accessing Google Mail
    1. Login to Google Mail
    2. Click on “Settings”
    3. Select “Always use https” in the “Browser Connection” section
    4. Save changes, log out and back in
    5. Verify that you are using a secure site (there should be a lock icon next to the URL)
  • Use a strong password that is unique from your other services
  • Do not provide access to your email to any 3rd party (no Facebook, MySpace, or LinkedIn). While it may be convenient to have these sites look through your contacts, it is like handing them the keys to your kingdom

If you follow these simple steps, you will make it much more difficult for people to hack into your email.