Seconds, Minutes, Hours – Converting Time Units in Ruby

I’m sure you’ve probably seen this question in your programming classes.. I decided I wanted to play with Ruby again and was remind of this problem, so thought it might be fun as a simple exercise to do this in Ruby.

The problem is to take a long integer and convert it to hours, minutes and seconds. This is a common issue in games, for example, where a countdown timer is represent internally in seconds. But players don’t want to see some huge number like 43678 seconds. They’d rather see 12:07:58 for hours, minutes and seconds. A couple of uses of the mod operator, and a couple of uses of the built-in formatter and you’re good to go.

Update: I wanted to clean up the code a bit more and put the ruby equivalent of a “main” via the __FILE__ check. This way you can just reuse the code via require if you so desire.

#!/usr/bin/env ruby

class TimeFormatter

  def format_time (timeElapsed)

    @timeElapsed = timeElapsed

    #find the seconds
    seconds = @timeElapsed % 60

    #find the minutes
    minutes = (@timeElapsed / 60) % 60

    #find the hours
    hours = (@timeElapsed/3600)

    #format the time

    return hours.to_s + ":" + format("%02d",minutes.to_s) + ":" + format("%02d",seconds.to_s)
  end
end

if __FILE__ == $0
  formatter = TimeFormatter.new

  puts formatter.format_time(43678)
end

require: command not found in ruby

While I was getting some old Ruby code of mine out to convert ruby code to pretty HTML-formatted code suitable for blogging from Wolfman’s Howlings I got the following error:

$ ./ruby2html.rb
./ruby2html.rb: line 9: require: command not found
./ruby2html.rb: line 10: require: command not found
./ruby2html.rb: line 11: require: command not found
./ruby2html.rb: line 14: syntax error near unexpected token `('
./ruby2html.rb: line 14: `    code= File.read(ARGV[0])'

Never saw this problem before, but obviously it was frustrating.

What had happened was I hadn’t deleted the tab spaces before the #!/usr/bin/env ruby line.

Apparently if there is any white space before the #!/usr/bin/env ruby the script will not run.

Swell.. :|

How To: Apple Boot Camp 64-bit for Windows 7 on “unsupported” Macs

How To: Apple Boot Camp 64-bit for Windows 7 on “unsupported” Macs

Do you own a Macbook Air or other modern Apple and want to install and use Windows 7 ala Boot Camp? If so, you may have encountered the dreaded “Boot Camp x64 is unsupported on this computer model” error dialog when trying to run the provided Snow Leopard Bood Camp setup.exe. Techulous has documented the workaround:

http://www.techulous.com/hardware/how-to-apple-boot-camp-64-bit-for-windows-7-on-unsupported-macs.html

Finding the Android Dev Phone 1 ROM Images

For some reason the ROM Images for the Android Dev Phone 1 have the links missing from the main ADP-1 download site. When you go to http://developer.htc.com/adp.html You’re greeted with a table of ROM images and file names but the links have been removed:

FirefoxScreenSnapz010.jpg

(You can click on the image to see the full-size version of the table.)

After some digging around I found that someone on the Android Developer List has found that the files are still on the website, only the links to them have been removed from the ADP-1 page. Creating the URL links are simple enough. You just start them with http://member.america.htc.com/download/RomCode/ADP and send with the file you want (i.e. ota-radio-2_22_19_26I.zip) The challenge here is that you cannot access those files directly, you have to be re-directed from http://developer.htc.com. If you’re a Linux or Mac user, this is solved with a simple shell script and the wget command. Modify the script for the ROM images that you want to download and you’re in business.

#!/bin/sh
IDIOTS1="http://developer.htc.com"
IDIOTS2="http://member.america.htc.com/download/RomCode/ADP"
wget "--referer=$IDIOTS1" "$IDIOTS2/ota-radio-2_22_19_26I.zip"
wget "--referer=$IDIOTS1" "$IDIOTS2/signed-dream_devphone_userdebug-img-14721.zip"
wget "--referer=$IDIOTS1" "$IDIOTS2/signed-dream_devphone_userdebug-ota-14721.zip"

If you’re a Windows user, you can accomplish the same thing if you have Cygwin installed: http://www.cygwin.com/


The script source for fetching the ROMs came from this discussion at The Android Developer mailing list. Thanks to Noah Tilton.

Installing Java 1.5 back into Snow Leopard

In an amazing moment of WTF I discovered this morning that after installing Mac OS X 10.6 (aka Snow Leopard) that the only version of Java running on it was 1.6. Now as an end-user, people probably won’t care. And I can kind of understand why Apple did it, given that Sun has done and End of Service Life for J2SE 5.0 (also known as Java 1.5). However a bunch of us still use earlier Java versions in active development projects. This was incredibly short-sighted of Apple. I’d guess that the 1.5 version probably exists on Snow Leopard Server Edition, but I’m not interested in paying for the server license when I don’t run OS X servers. However, it’s possible to bring back Java 1.5 onto your Snow Leopard installation. There are a few other pages that explain how to do this but I’ll re-iterate the steps I used here because it uses the most recent J2SE 5.0 release:

Preparation:

First, get Java for Mac OS X 10.5 Update 5 from Apple.

Next get Pacifist from CharlesSoft. This is a great utility. It’s saved my butt a few times. Especially when I needed to get something from an Apple installation package without having to re-run the installer. This qualifies as one of those times. If you don’t already have a registered copy, get one. It’s worth US $20.

How to install.

1. Use Finder to go to System > Library > Frameworks > JavaVM.framework > Versions and delete the two aliases (symlinks) "1.5" and "1.5.0". If you skip this step you will lose your Java 1.6 installation that comes with Snow Leopard. That would be bad. So don’t skip this step!

2. In finder, open JavaForMacOSX10.5Update5.pkg that’s inside JavaForMacOSX10.5Update5.dmg with Pacifist. Drill down to Contents > System > Library > Frameworks > JavaVM.framework > Versions.

3. In Pacifist, select 1.5 and 1.5.0, right-click, and chose Install to Default Location.

You can probably do the same thing with the 1.4 installation if you need it. I didn’t try.. but it would probably work.

Update 12-4-2009:After installing the latest Java update for OS X 10.6 I was greeted with the following change to my Java Frameworks directory. FinderScreenSnapz003.jpg

Apparently the updater saw fit to move my Java installation for 1.5 aside during the update and recreate it’s own aliases/symlinks. If this happens to you follow step one, and rename the 1.5.0 1 folder back to the original name.


Thanks to the following articles from which I borrowed and stole heavily…

- chxo internets
- oneswarm.org
- daveyshafik.com

Subversion and Snow Leopard

Along with so many others, I upgraded to Snow Leopard. Overall the upgrade went without a hitch. However, I noticed that my Subversion repository was no longer available from Subclipse or via the Web Browser. Not good.

So I did some digging around and upon finding this article from Patrick Rice http://patrick-rice.net/daybook/2009/09/20/subversion-snow-leopard-etc/ I was up and working again in a few minutes.

Apparently with the Snow Leopard upgrade, the Apache mod_dav_svn configuration was removed from /etc/apache2/other/svn.conf. Patrick references the following article. It’s extremely educational and informative: How To: Manage Your Own Subversion Repository In Leopard. The details still apply in Snow Leopard, as well.

Following these articles I just created a new /etc/apache2/other/svn.conf.

LoadModule dav_svn_module /usr/libexec/apache2/mod_dav_svn.so

<Location /svn>
    DAV svn

    SVNParentPath /Users/Shared/svn

    AuthType Basic
    AuthName "Subversion repository"
    AuthUserFile /etc/apache2/svn-auth-file
    Require valid-user
</Location>

Restart the Apache server (via Sharing in the System Preferences application). And you should have your repository back.

Bloo – the Facebook app for Android. Now on the Android Market

If you use and Android device like the T-Mobile G1 and you haven’t heard of Bloo yet, you will. This is probably the slickest application for Facebook on *any* platform. Excellent photo album and photo upload support, a slick user interface a small memory footprint, and it’s obviously clean, tight code judging from how fast it is. Configuring it does need a few simple steps that are all documented on his website. Use Facebook? Got the T-Mobile G1? Get this app. It’s a steal at the $1.49US price tag.

Useful Links for Getting Started with Bloo:

RIAA goes off the deep end. Fines a woman 1.92 million for sharing 24 songs… :P

From the article:

Does a $1.92 million award for sharing 24 songs cross the line into ‘grossly excessive’?

Does a bear !@#$ in the woods?!!!?

Analysis: $1.92M fine in music piracy case could hurt RIAA – iPod/iPhone – Macworld UK

Pontiac 2010 Solstice Orders Cancelled

It’s very sad to read this. The Solstice GXP is one of the best cars that GM has put out in the last 5 years. However, it’s not surprising given the bankruptcy of GM, and the announcement of Pontiac’s demise. But still, you’d think GM would want to sell these cars a *little* longer. Hopefully someone will consider buying the delaware plant and continue making this excellent car under a different moniker. It’s probably a naive hope, however.

To: Select Pontiac Dealers and Sales Managers

Subject: 2010 Model Year Solstice Orders Cancelled

On June 1st, GM announced the closure of the Wilmington Delaware plant which produces the Pontiac Solstice. All 2010 Solstice in-system orders will be cancelled today. All 2010 ordering materials in Order WorkBench and the On-Line Order Guide will be removed from the site. All 2009 Model Year Solstice orders have been received. Build out of the 2009 Model Year for Solstice will be July 28, 2009.

YellowPages.com Sucks

For the last year my wife’s small-but-growing, word-of-mouth based business has been listed with YellowPages.com. The Networking Group my wife was a member of included in their ranks a sales rep who pushed that this would be an outstanding way to expand her business. That they could setup an online coupon with my wife’s listing and people could just print it and walk in with it. All the typical sales rhetoric.

My wife felt that the momentum of her business (which was doing OK) could use a boost and decided to invest in advertising with YellowPages.com as an attempt to expand and grow her business. Contracts were signed, paperwork was filed, paperwork was turned in for the online coupon, and charges to my wife’s business card started piling up.

And as a result my wife’s business was listed on YellowPages.com.

Now, in the past year there certainly was an increase in traffic to my wife’s website. I have a statistics collection system running on her web server. And she got probably an additional 3-10 referrals via YellowPages to her website each month.

However, because of the lackluster and incompetent effort on the part of the sales rep we worked with (who’s now a district sales manager) my wife’s legitimate health care practice was listed along side escort services.

Why Yellowpages.com Sucks

Nice…!

As a result those YellowPages.com referrals resulted in two calls. Those two calls resulted in requests typical of what escort services would provide (even tho for legal reasons they say they do not).

Pan forward a year… My wife, near the end of our contract, lo-and-behold gets a call from a new Yellowpages.com rep!! And he wants talk about renewal options for another year. My wife informs him that is not going happen. He goes into the usual rhetoric that sales reps right now are trying to use in the face of our current recession; ‘Well, with this recession going on it will be impossible for your business to survive without us!’. When she explains the lack of the online coupon, the lascivious phone calls, it’s at this point in time he’s willing to get the listing re-categorized but only in the face of certain discontinuation of a contract. When my wife asks about the online coupon he says; “Well, you needed to turn in the paperwork for that”. My wife said; “I turned in the paperwork for that [to the previous rep] when I signed up for service and paid all this money!” He replies; “Oh, well then there’s nothing I can do about that. Sorry.”

He tries every angle and pressure tactic he can think of and even offers a meager discount for the following year worth of service. Even though she didn’t get what she paid for to begin with.

At this point, if YellowPages.com wants my wife’s listing for another year, they can pony up a year’s worth of free service. They simply didn’t do their job and actually possibly did *damage* to my wife’s business considering it looks like it’s a damn prostitution service.

If you’re with YellowPages.com and you’re reading this, you and your company suck. Big time.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes