Refactoring ruby code

July 11th, 2008

I am currently working on a Rails project with a team of developers.
I did some refactoring of a helper method and thought I would share it with the world.
Maybe someone out there can benefit from this.

NOTICE: I changed the helper method name and the model[attribute] values to show a more generic method.

Before refactoring

def some_helper_method
select_tag 'model[attribute]‘,
options_for_select([[" Today ... ", Date.today], Date.today+1,
Date.today+2, Date.today+3, Date.today+4, Date.today+5,
Date.today+6, Date.today+7])
end

After refactoring

def ship_date_select(model,attribute)
todays_date = Date.today
options = [['Today ... ', todays_date]]
6.times do |x|
options << todays_date+x
end
select_tag "#{model}[#{attribute}]", options_for_select(options)
end

The problem with the first example is that it is not very DRY. It is asking Ruby the date 7 times.
It should only need to ask this once and store it in a variable called todays_date. If anyone out there knows a better way to do this please feel free to shoot me an email.

Best,

Tim M.

Meet the Aqua Bots

May 16th, 2008

Meet the Aqua Bots

A pair of underwater robots I created.

Gaffney Plumbing - Marshfield Wisconsin Plumbing Company

May 15th, 2008

gaffney-post.jpg
Marshfield Wisconsin Plumbing Company

HowTo: Shutdown your Mac with a text message

April 21st, 2008

Ever went on a trip and thought oh shit I forgot to turn of my Mac? Well you could turn around and go back, you could call a friend or you could just send a text message to your email address that Mail downloads and shut it down yourself. I’ve already showed you how to shutdown windows with a text message and a batch script. Now it’s time to do it Mac style. To complete this tutorial you will need the following.

1. A Mac (duh) Ive only tried this on Leopard, if you get it running on Tiger please let me know.
2. Mail which if you have a Mac you should have installed.
3. ScriptEditor also included with Mac
4. A cellular phone with sms text messaging capabilities and sms service.
Read the rest of this entry »

Holistic Dog Food - Better for your dog

April 5th, 2008

Domino My Puppy
Hello everyone,

I have recently switched my dog over to holistic dog food products to see if it makes any difference at all and it does. Domino has been on holistic dog foods for about 2 months now and his coat is nice and soft and very shiny, his teeth seem to be whiter and he has not been chewing up my things as much lately. I get my holistic dog food from work as I work for a pet store in Kenosha WI. The reason I wanted to let everyone know this is I didn’t think it would make that big of a difference. Yet it did after only a few weeks. If you wanna see the difference you can buy holistic pet food for both dogs and cats by clicking the link below.

www.barksnbubbles.com

HowTo Juice a lemon (Juicy Post)

January 26th, 2008

If your anything like me your not so handy around the kitchen. No worries, you can obtain master cooking skills from various sites around the internet and with a little practice, you can improve your cooking skills. To get you started heres a great video on how to squeeze a lemon to optimize juice production when juicing.

www.startcooking.com

DIY Desktop Virtual Reality Display on the cheap (Nintendo WII Only)

January 26th, 2008

Johnny Lee, and his WIImote hacking tips has created a way to make 3D games (for the WII) a reality using DIY IR glasses. This is a really cool concept, the software tracks the movement of your head and adjusts the current display accordingly. I am definitely going to experiment with this technology.

You can also check out Johnny Lee’s website here.

1 Year of Ruby on Rails Programming

December 21st, 2007

Over the past year I have made the change to Ruby on Rails as my programming language and framework of choice. The reason is simple, Ruby on Rails manages enough of the tedious tasks faced by most developers fairly easy. With that said it is also safe to say it makes me more productive, and increases the quality of my work. The pros outweigh the cons by far. I would encourage anyone programming in PHP, ASP/ASP .NET, C+, C++, JAVA, or even Cold Fusion to give Ruby on Rails a shot. Most likely you will hit the Rails and never look back, yeah it’s that great. It has now been one year since I originally Installed Ruby, Installed Rails and dug deep into the code. While I am still learning a great deal about some of Ruby’s more advanced functionality I have mastered for the most part the entire framework from unit testing to model associations. If you are just getting started in Ruby I would be glad to answer some of your questions, simply send me an email or comment on this post below.

Best regards,

Tim Matheson

SSH Copy files from one server to another using rsync clie tool

December 12th, 2007

Ok so you have a file and want to move it to another server on another network. What to do what to do. Well you could just FTP it to your local machine then FTP it up to the new server, but this file is 300mb and that would take a long time. Being the lazy programmer you are you may seek another more ‘geek friendly’ way of doing this. There is a tool called rsync that does just that. It will login to one server, shake hands and then begin transferring your data to the specified new directory. I tested this on BlueHost and no dice. Authentication failed however I am sure if you have time to try and configure the two machines to talk you could do it. This would be great if you had root access to both machines. In my case I only have root to the destination server and not the source server. BlueHost technical support said that the port rsync uses is blocked. Im not sure what that port is but if you found out you could configure rsync as a daemon service and bind it to that port. I will be looking into this in the future and posting more about it in a follow up post.

Happy tunneling.

rsync -av -e ssh username@oldserverhost:/home/old_home/ /home/new_home/

This command was gracefully copied from [Tony Spencer's Website] it’s a great website I highly recomend it if you like my site (even has the same ndesign Word Press theme as my blog).

New Ruby on Rails site

December 6th, 2007

I recently deployed a few Ruby on Rails sites to a production server. If anyone wants to check one of them out they can visit Barksnbubbles.com Dog Food, Dog Toys, and Pet Supplies online retailer. If you have any suggestions for the site please let me know.

Best regards,

Tim Matheson

New Red Interactive Codes for Hidden Characters

November 27th, 2007

ff0000.png
I recently revisited Red Interactive and found some new characters in the sourcecode of the flash file. I thought I would share them with you here.

“nay nay give me some hay” = “ed”

“dead men tell no tales” = “nub”

“jiminy cricket!” = “hopper”

“the last straw” = “cameljoe”

“surley you jest” = “jester”

“rumpelstiltskin is my name” = “unclesam”

“may i please have some blueberry pie?” = “gordon”

“i shot a man in reno” = “jailbird”

“i dreamt i was a butterfly” = “sun”

NEW CODES!

“happy halloween” = “skely”

“the funk of forty thousand years” = “skeletor”

“trick or treat” = “shakspeare”

“smell my feet” = “minuteman”

“baby got back” = “fatback”

“nay nay i never got any hay” = “dropdeaded”

Surface computing brings multi touch sensors to your home

November 27th, 2007

I have been following this multi-touch sensor based computer technology pretty closely. I am fascinated by the thought of these types of devices to replace our current constraint prone desktop and laptop computers. The file transfer feature has got to be one of the coolest features of the coffee table computer shown in the video. I dream of a day when wads of cables become non-existent.

Send emails with ease, Rails Mailers Demystified

November 25th, 2007

Ruby on Rails Mailers

Setting up a mailer in Rails can seem like a daunting task, especially to new comers. I found this video and several other resources online to be very helpful in explaining the ins and outs of setting one up. This is a very in depth video explaining how mailers can make life easier for the Ruby programmer. See the screencast!

[via : http://media.railscasts.com/]

Use Hibernate and Standby to Conserve Batteries

November 24th, 2007

Microsoft has a great article on how to conserve your laptops battery supply using hibernate features available in “most” windows installations. I really think this is an overlooked feature available to Microsoft customers. Read the full article

  1. Open Power Options in Control Panel. (Click Start, click Control Panel, and then double-click Power Options.)
  2. Click the Hibernate tab, select the Enable hibernate support check box, and then click Apply.
  3. If the Hibernate tab is unavailable, your computer does not support this feature.

Stream your files anywhere anytime for FREE

November 23rd, 2007

ORB Media Freedom
www.orb.com is a FREE piece of software that allows you to stream your desktop computers content to various web enabled devices such as cell phones, laptops, pdas and even other desktop computers remotely. I especially like the feature which allows you to stream video to your smartphone. While buffering can become excessive at times, the stream makes up for it in quality and the ability to encode virtually any media file you through at it avi, mpeg, mpeg4, mov, wmv etc. Orb is a must have for an on-the-go lifestyle. Get it FREE here

UPS Shipping Gem not working - Fixed

November 4th, 2007

Hello everyone,

I recently had the joy of working with the Ruby Shipping gem which allows you to grab shipping rates from UPS and FEDEX. However many people (myself included) have had problems with this gem. The reason is simple the documentation contains misinformation and is causing many developers to bang there heads. I found a solution and would like to share, so here it is.

OK first off make your life easier and create a file
/config/shipping.yml and add your ups account information as shown
below.

#in your config/shipping.yml file
ups_license_number: ups_xml_access_key
ups_user: ups_username
ups_password: ups_password

One important thing to notice is that I am using ups_license_number: as
opposed to ups_account: as shown in the Shipping gem docs :( . This is
what is causing all the trouble. Also note that I am using the xml
access key and not the account number.

Next you need a method to get your quote. Here is a quick one to get you
started.

#in your controller
def ups_quote(to_zip, sender_zip, weight)
  #define the parameters for the quote
  params ={
  :zip => to_zip,
  :sender_zip => sender_zip,
  :weight => weight,
  :prefs => "#{RAILS_ROOT}/config/shipping.yml"
  }
  #build the shipping gem object
  @shipping = Shipping::UPS.new params
end

#in your view

Shipping Quote: <%= number_to_currency @shipping.price %>

Call for help! Ruby - On - Rails Shipping Gem

October 17th, 2007

Hello everyone, I have been programming in Ruby using the Rails framework for a while now. However I have a situation where I am developing a shipping calculator for my applications using the Ruby “shipping gem”. The problem, at least I think, is the account number, username, password or all of the aforementioned attributes are incorrect. I have verified they are indeed correct and valid. I am getting an object returned to me in my code called the Shipping::ShippingError object. I am sure there is some kind of a message stored inside this object but I cannot access it because I think it is actually a Class. In short what I need to know is how do I, if at all extract the error from this Class? Object? so I can display it to the screen. Below is a code example.

The configuration file “shipping.yml”

#../../config/shipping.yml
ups_account: XXXXX
ups_user: XXXXX
ups_password: XXXXX

The Shipping Controller “app/controllers/shipping_controller.rb”

#app/controllers/shipping_controller.rb
class ShippingController < ApplicationController
   def quote
      params = {
                     :zip => 54494,
                     :state => “Wisconsin”,
                     :weight => 1,
                     :sender_zip => 68801,
                     :sender_state = “Nebraska”,
                     :prefs => “#{RAILS_ROOT}/config/shipping.yml” }

      @shipping = Shipping::UPS.new params
      @errors = Shipping::ShippingError
      #in the view we will show price like so @shipping.price
   end
end

The view file “shipping/quote.rhtml”

Test Shipping
This is a test of the UPS shipping code.
Output = [<%= @shipping.price %>]

Open for web development projects

September 16th, 2007

If you have a web development project relating to my work please contact me. I prefer to work with Ruby on Rails projects however I am additionally trained in PHP in a mySQL Apache environment.

Looking forward to working on your project.

Tim Matheson

Browse photos in a whole new way with Photosynth from Microsoft!

June 15th, 2007

photosynth.jpg

Photosynth is an online pilot application for some new photo viewing technology from Microsoft Live Labs. Photosynth takes a collection of photos of a place or object and uses a computer algorithm to arrange the photos in the order they would appear in the real world. This creates a virtual 3d representation of the object or place in the photos. Currently Photosynth appears to be a test application providing several test collection of places. The project does not currently allow you to use your own photos. However it is a very nice little piece of software and I would encourage you to check it out.

Link to: Photosynth website

ff0000 Red Interactive - Hidden Character Codes

June 3rd, 2007

ff0000.png

If you want to try a fun interactive chat visit Red Interactive. You can chat, and punch people, and even fly. This is a very fun site to visit. There are hidden characters in the site to. After some research I came up with all the codes which I am listing below.

Type any of the following codes into the chat message box exactly as shown on the left side of the equals sign below. Example if I want the camel I type “the last straw” exactly (without the quotes) into my message box and press enter ad my character will change. Have fun.

nay nay give me some hay = ed (Horse)

dead men tell no tales = nub (Pirate)

jiminy cricket! = hopper (Bug)

the last straw = cameljoe (Camel Man)

surley you jest = jester (Jester)

rumpelstiltskin is my name = unclesam (Uncle sam on stilts, he wont speak a word)

may i please have some blueberry pie? = gordon (Overweight man)

i shot a man in reno = jailbird (Bring back the convict, man with ball and chain)

i dreamt i was a butterfly = sun (Woman character that was removed, carries two fans)