Pass Optional Arguments to Ruby Method

written by Jerod on July 24th, 2008 @ 05:04 PM

This the Ruby way of passing optional arguments with default values into a method:

   1  def awesomeness options = {} 
   2    #sensible defaults
   3    opts = {
   4      :name => "Jerod",
   5      :handle => "sant0sk1",
   6      :blog => "Standard Deviations"
   7    }.merge options
   8    
   9    opts.each { |key,value| puts "#{key} = #{value}" }
  10  end

When called sans arguments this function will print the following:

   1  awesomeness
   2  handle = sant0sk1
   3  name = Jerod
   4  blog = Standard Deviations

When called with arguments this function will merge them into the opts variable and print the following:

   1  awesomeness :name => "Dork"
   2  handle = sant0sk1
   3  name = Dork
   4  blog = Standard Deviations

The defaults are used unless you specify an override in the method call, in which case the override is merged into the opts hash.

Rails + Screen = Awesome

written by Jerod on July 24th, 2008 @ 10:38 AM

Unix’s screen utility is like pow-pow-power wheels for remote shells. I created a short screencast awhile back showing how to use screen to ease your Rails development.

The server that was hosted on is gonezo so I am reposting it here for your enjoyment:

Link to Embedded Screencast

Symlink Your Samba Shares

written by Jerod on July 23rd, 2008 @ 04:35 PM

Lets face it, oftentimes a symbolic link is just the quickest/easiest solution to the task at hand.

To configure Samba to allow symlinking directories/files into your shared directories, add the following three lines to the global section of smb.conf:

   1  follow symlinks = yes
   2  wide symlinks = yes
   3  unix extensions = no

Easy peasy lemon squeezy.

Centering Images Using CSS

written by Jerod on July 21st, 2008 @ 09:21 AM

Posting this for my own reference. I always forget how to do this and am sick of looking it up:

   1  <!-- html file -->
   2  <img class="example" src="/path/to/image.png" />
   1  /* css file */
   2  img.example {
   3  display: block;
   4  margin-left: auto;
   5  margin-right: auto;
   6  }

Its the block display setting that I always forget. Not anymore baby!!

ip2loc

written by Jerod on July 19th, 2008 @ 11:11 AM

ip2loc

ip2loc is a simple tool which visualizes location data for a given IP address using the Google Maps API. I created it for a few reasons:

  1. Most sites that do this charge or are clunky
  2. I wanted to write a simple application with Sinatra
  3. It was a good excuse to sharpen my jQuery skills
  4. Boredom

ip2loc is powered by Ruby, written in TextMate, and deployed with Passenger on DreamHost.

My Perfect OS X FTP Server

written by Jerod on July 17th, 2008 @ 02:47 PM

I don’t like FTP. Reasons abound, but to list a couple:

  1. Too complex to configure
  2. Insecure by default

But lets face it, sometimes you need to set up a quick and dirty FTP server for one-time use. Sure, OS X supports FTP file sharing natively but its a bit clunky because you have to either A) allow Anonymous access, or B) create a user account on your system and set up sharing on it. Lame.

There are many freeware,and shareware FTP clients but not too many servers. After a cursory review of all the offerings on IUseThis, I settled on PureFTPd Manager which is a free and open-source front-end for PureFTPd…and what a great choice I made.

Why does this app rock my socks off?

  1. Dead simple configuration - nice GUI walks you through everything
  2. Virtual users - one new system account and endless virtual users it can represent
  3. Logs and live status updates from GUI
  4. Uninstalls in a few clicks
  5. SSL/TLS support

Traversing Directories with Ruby

written by Jerod on July 15th, 2008 @ 03:32 PM

If you want to shove filenames of all files in a directory into an array, do:

   1  # (absolute path)
   2  files = Dir["/Users/jerod/src/**"]
   3  # (relative path)
   4  files = Dir[File.expand_path("~/src") + "/**"]
   5  # (in ENV["PWD"], aka current directory)
   6  files = Dir["**"]

If you want to shove filenames of all files in a directory recursively into an array, do:

   1  # (absolute path)
   2  files = Dir["/Users/jerod/src/**/**"]
   3  # (relative path)
   4  files = Dir[File.expand_path("~/src") + "/**/**"]
   5  # (in ENV["PWD"], aka current directory)
   6  files = Dir["**/**"]

It can’t get much easier than that.

New GitHub Repo: AWS Ruby Miscellany

written by Jerod on July 14th, 2008 @ 02:09 PM

I decided to source out a few random Ruby scripts I use to interface with Amazon’s Simple Storage Service (S3) from the command line. Right now there are scripts which allow:

  • Duplicating a local folder in a specified S3 bucket (recursively)
  • Listing all buckets and files stored on an S3 account
  • Uploading a specified file to a specified S3 bucket
  • Downloading a specified file from a specified S3 bucket.

I’m sure the functionality will grow as needs arise. For now, check out the repo on GitHub

Capify - public key deployment

written by Jerod on July 11th, 2008 @ 09:12 AM

I’ve been playing with Capistrano a lot lately and loving it. Here is an example of how easy it is to write tasks and use them on multiple remote servers.

This task installs your SSH public key on the remote machine to allow key-based authentication:

   1  set :key_file do
   2      Capistrano::CLI.ui.ask "enter public key to push: "
   3    end
   4    
   5    desc "configures key-based SSH administration"
   6    task :push_key, :roles  => :all do
   7      key_location = File.expand_path(key_file)
   8      unless File.exist?(key_location) and key_file.match(/\.pub$/)
   9        puts "Couldn't locate public key. Try again"
  10        exit
  11      end
  12      key_file_name = File.basename(key_location)
  13      upload key_location, "/tmp/#{key_file_name}"
  14      run "if [ ! -e ~/.ssh ];then mkdir ~/.ssh; fi"
  15      run "cat /tmp/#{key_file_name} >> ~/.ssh/authorized_keys"
  16      run "rm /tmp/#{key_file_name}"
  17      run "chmod 600 ~/.ssh/authorized_keys"
  18    end

Silky smooth.

SliceHost + Debian + FireHOL gotchas

written by Jerod on June 20th, 2008 @ 11:44 AM

Securing your slice with FireHOL is a really, really good move. Here are a few notes that may save you some time:

  1. FireHOL requires a kernel config to know which modules to load, SliceHost uses Xen, so to get the kernel configuration in the right place, execute the following commands:

       1  cp /proc/config.gz ~ && cd ~
       2  gunzip config.gz && mv config /boot/config-`uname -r`
    
  2. FireHOL ships with a safety net configured in /etc/defaults/firehol. It will not start until you edit this file and change this:

       1  START_FIREHOL=NO
    

    to this:

       1  START_FIREHOL=YES
    

That should do it for gotchas. Now you can lock down your machine to assure you’re only serving what you expect. Fore more on configuring FireHOL, check out their online tutorial

Rails: Nested Layouts

written by Jerod on June 20th, 2008 @ 09:59 AM

Sometimes one layout (application.html.erb) just doesn’t cut it, but you don’t want a separate layout for each controller in your app. You can use the following technique to nest your Rails app’s layouts:

In this example, there are two controllers for a simple todo list manager; lists and tasks

   1  #  application.html.erb
   2  
   3  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   4    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   5  
   6  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   7  <head>
   8    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
   9    <title>TODO >> <%= yield(:title) || "Get things done!" %></title>
  10    <%= stylesheet_link_tag 'todo' %>
  11  </head>
  12  <body>
  13    <div id="container">
  14      <p style="color: green"><%= flash[:notice] %></p>
  15  
  16      <%= yield :main%>
  17    </div>
  18  </body>
  19  </html>

The key here is the yield :main on line 16 which means we can use content_for to put our nested layouts’ output inside the yield. Here is the lists layout:

   1  # in lists.html.erb
   2  
   3  <% content_for :main do %>
   4   <div id="lists_container">
   5    <%= yield %>
   6  </div>
   7    <% end %>
   8  <%= render :file  => "layouts/application.html.erb" %>

Now your view gets rendered inside this layout’s yield and its all good in the hood. Cheers!

Including jQuery in your Rails app

written by Jerod on June 12th, 2008 @ 09:16 AM

If you’re loving jQuery for one reason or another (simple DOM manipulation for one) and you’re thinking about using it in a Rails project or three, this little gotcha may have you scratching your head.

Let’s say you’ve been using the default javascript libraries and writing all your little functions in application.js like a good boy, then you decide to include the jQuery library and play around. It probably looks something like this:

   1  <%= javascript_include_tag :defaults, "jquery" %>

So you code up some jQuery hackery in application.js and are having so much fun that you decide to ditch the default libraries outright. Yea Yea! You change your include to look like this:

   1  <%= javascript_include_tag "application", "jquery" %>

Suddenly none of your jQuery functions are firing! What What What?!?!

The order of javascript includes matter, so you have to load your jQuery library before loading your application.js file. Change your include to look like this:

   1  <%= javascript_include_tag "jquery","application %>

Ahh, that’s better!

If you’re wondering why it was working before when you were loading the :defaults before the jQuery library, its because Rails is smart enough to know that application.js needs to be loaded last no matter what, so it will load all the libraries and then append application.js to the end. Very nice.

3G IPhone Releases July 11th

written by Jerod on June 9th, 2008 @ 02:28 PM

And my birthday is July 12th. Hmmmm…..

htop: like top, but awesome

written by Jerod on June 3rd, 2008 @ 09:05 AM

If you’ve been hacking at your linux CLI for a little while, you’re probably familiar with the pic below. If not, just type ‘top’ (no quotes) and your terminal will spring to life with an ever-updating process and usage display.

While very useful, top’s output is oogly. It’s the 21st century already! Let’s use some colors!!

Thankfully, htop is here to save our day. To install on a Debian-based linux, simply type:

   1  sudo apt-get install htop

now we should have htop installed at /usr/bin/htop. Let’s add a quick alias so we don’t accidentally launch top anymore. Open your .bashrc in your favorite editor and add the following code to it:

   1  if [ -f /usr/bin/htop ];then
   2       alias top='htop'
   3  fi

Now whenever you start a terminal session and type top you’ll launch htop instead (but only if htop is installed on the system). Quickly tell bash to re-read your configuration file and try launching htop!

And there’s the awesome

10.5.3 Fixes Spaces

written by Jerod on May 31st, 2008 @ 08:46 AM

If you’re like me then one desktop just doesn’t cut it anymore. Leopard’s Spaces is Apple’s attempt at a virtual desktop management app and it has lacked one very crucial feature until the .3 release of the operating system which was pushed last week.

Its a difficult feature to explain but it basically allows your to divide your spaces by task instead of by application. I was dying for this fix because everytime I would switch to an application in a space that didn’t have it open already, I would be whisked away to another space that I did not want to be in.

To activate this feature just open the Exposé and Spaces preferences pane and unclick the following checkbox:

Read Gruber’s post on this for a more eloquent and thorough explanation.

Options:

Size

Colors