In what I can think of as the most important JavaScript that I have ever written, I have made something which will parse the DOM and replace all of the ‘l’s with ‘r’s. This idea came about after watching Team America: World Police about 1,000 times and after Kim Jong Il gave his apology for doing some nuke testing. Back to the script:
function engrish(n) {
if(n.nodeType == document.TEXT_NODE) {
n.nodeValue = n.nodeValue.replace(/l/g, 'r').replace(/L/g, 'R');
} else if(n.hasChildNodes()) {
for(var i=0; i<n.childNodes.length; i++) {
engrish(n.childNodes[i]);
}
}
}
To execute this code, simply call engrish(document.documentElement);. Using recursion, this function goes through the DOM, finds text nodes and replaces the characters.
Read the rest of this entry »
This is a fairly simple little tidbit, but still useful. Sometimes I want to have a script for my rails application, like running some reports on the data. There are two basic ways to do this: write a task(rake), or create a new ruby script file, like in the scripts directory.
If you want to do the latter, then there are two lines you my want to put at the top of your script.
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || ‘development’
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
The first line allows us to supply which environment we would like to run the script in. Say the script is called process.rb, we could call the script like ./process.rb production and now the environment is set to production.
The second line loads the environment file. Now all of rails, your models, libraries, and plugins are available to you.