Everyone make sure to run this handy, free Conficker worm removal tool before it’s too late. I think it’d be cool to see some Live Free or Die Hard-style hacking tomorrow, just not on my machine.
Archive for March, 2009
Date Formatting in Rails
March 25th, 2009This is a helpful cheat-sheet for date formatting syntax for Ruby. Good for doing something like this:
Time.now.localtime.strftime("%A, %B %d, %Y")
Would print, “Wednesday, March 25, 2009″.
Also, if you’re using Rails, you can get the day-suffix by using the handy little rails function, ordinalize:
Time.now.localtime.strftime("%A, %B #{Time.now.localtime.day.ordinalize}, %Y")
will print, “Wednesday, March 25th, 2009″.
This works for any integer, as well:
3.ordinalizePrints, “3rd”.
Transfering files via the X clipboard
March 25th, 2009In X systems you can copy text by highlighting it with the mouse. This automatically copies it to the “primary” clipboard. You can paste it with a middle mouse click(right click if you’re using putty I believe). This becomes so fast and natural that Ctrl+C/Ctrl+V starts to feel painfully difficult in comparison. Frequently I want to quickly copy the contents of a file from one server to another. In the past I’ve done this with:
[skh2@webdev-rhel4 mip]$ cat agreement-form.html
Then I select the output with the mouse and paste it into a file on another computer. Selecting the text of the file is tedious for longer files. I thought there must be a way to transfer the contents of a file directly to the clipboard, and it turns out there is!
http://sourceforge.net/projects/xclip
After installing this I can do:
[steve@box mip]$ xclip agreement-form.html
Then move to another computer and redirect the clipboard contents to a file:
[steve@otherbox mip]$ xclip -o > agreement-form.html
All too easy!
Rails with Phusion Passenger and Oracle
March 23rd, 2009Note: The technique listed below to get Oracle ENV vars to Rails is out-of-date as of Passenger 2.2.3. See the updated post.
A big step in any Rails newbie’s lifetime is moving from the built-in WEBrick server to an actual (fast) production-style server setup. Since we use Apache here at SHA, the obvious option was to use Phusion Passenger’s Apache module.
Now, this is supposed to be a filthy-easy way to serve rails apps. And it probably is, for most people. But here at the SHA, we have a saying “nothing ever works the way it’s supposed to”. I guess it’s not really a saying, but more of a shouting, and it’s usually followed by a stream of angry profanity, and it’s mostly me that does it. But it’s kindof a saying. Regardless, as usuall, installing Passenger via:
gem install passengerand
passenger-install-apache2-module
Didn’t work the way it was supposed to. Granted, there were a few complicated configuration issues that were unique to our setup, but there was an extra step that we had to implement due to our use of the Oracle DB server.
You see, usually you just need to add this to your Apache configuration:
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.1.2 PassengerRuby /usr/local/bin/ruby
Doing that, though, we were getting errors that pointed to Ruby not seeing the standard oracle db drivers. We eventually found this site that described how to get Passenger to start Ruby with Environment variables, which basically consists of making a little script called ruby_with_env:
#! /bin/bash export ORACLE_HOME=/path/to/oracle/product/10.2.0/db_1 export LD_LIBRARY_PATH="/usr/lib:/other/library/path/info/lib:$LD_LIBRARY_PATH" export NLS_LANG="AMERICAN_AMERICA.AL32UTF8" export TNS_ADMIN="/path/to/oracle/product/10.2.0/db_1/network/admin" /usr/local/bin/ruby $*
Where /path/to/oracle/product/ is the local path to your Oracle server. This differs slightly from the linked page’s environment settings, as we needed to set the ORACLE_HOME var as well as our language and admin settings.
Finally, we change the passenger references in our Apache config file to read:
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.1.2 PassengerRuby /usr/local/bin/ruby_with_env
And it’s worked great since then.
Javascript Madness
March 19th, 2009Google launches a new site to demonstrate the absurdly fast JS engine in Chrome. If only…
Quick var dump in Rails Controllers
March 16th, 2009So, coming from a background in PHP and especially CakePHP, I used to do this a lot in my controllers:
print_r($some_var); exit;
Just to do a quick spot-check on the data going through the controller action.
If you’ve noticed, doing something like this in a Rails Controller gives no output at all:
puts some_var.inspect
Good for Rails! So you could always use the logger.info() function, or, to absolutely resist changing everything about how I code, this works, too:
render :text => some_var.inspect
Note that you’ll have to comment out any other render() methods in the action. But it’s a good, quick way to see what you’re data is up to.
Fresh Soup vs. Grease
March 12th, 2009How come Goldwyn-Smith hall gets Zeus and the Hotel School gets Mac’s “cafe”?
Just asking.
Apache Does NOT Read Query Strings as part of the URL
March 4th, 2009To spare anyone else the pain of spending a morning trying to get an Apache RewriteRule to match patterns in a query string (anything follwing the “?” in the URL), Apache does not consider the query string as part of the actual URL. Makes sense, I suppose, since the URL technically points to a single resource. But STILL.
This is why you never see examples of people using rewrite rules to map this:
http://www.example.com/index.php?var0=2&var1=3&var5=hey
to something like this:
http://www.example.com/var0/2/var1/3/var5/hey
Rails API is for Edge Rails
March 2nd, 2009…so I found out, after wondering why my ActiveRecord::update_counters() method was not working with arrays of IDs. The API says Rails supports this, but the actual code in version 2.2.2 obviously does not.
Handily, there is no mention of this on the Rails API site.