• VCR for shell scripts ·

    The VCR gem is really handy for testing calls to external web services, such as AWS commands made through Fog. You make your call for real once and VCR records a new “cassette” of the request and response. Subsequent calls use the cassette, so VCR plays back the response it recorded instead of actually reaching out and touching the external service. It’s good for testing how your own program makes requests and how it handles the responses it gets back. I recently had a need for similar functionality but with command line tools. Namely, the ec2-cmd script for importing an instance into Amazon.

  • a Boolean query parser in Python ·

    Last summer I worked on a project where I needed a search engine that supported simple Boolean queries and key-value parameters. This was to be used on a Python site, so I wanted to write the query parser in Python too. At the time, Lepl seemed like a good choice. I figured out the grammar necessary to support the search query language I wanted, and all was good. I went to check out Lepl just now and I see it has been discontinued, which is a little disheartening. Regardless, I figured better late than never to post this since it still works and Lepl did the job.

  • Ruby Matrix and Vector additions ·

    Working with matrices and vectors in Ruby before has been annoying because methods I expected to find were not there, and there wasn’t much documentation. Here, I present some simple additions to the Matrix and Vector classes that I found helpful.

  • convenient file searching with Ruby, grep, and file ·

    For my Linux kernel class, I often know that some struct exists somewhere, or remember seeing a macro defined in some file and it might be useful, but I can’t remember where I saw something. I also end up trying to track down all the places a particular function is called, and don’t want grep to go digging through every… single… file in the entire kernel directory structure when I only care about .c files. So, I dug up a lengthy combination of file and grep that limits grep’s searching to particular files. I’m lazy about remembering this and retyping it on different computers, too, though, so I wrote a quick Ruby script to do it for me:

  • representing rational numbers in Smalltalk ·

    For my graduate-level programming languages class, I wrote this class that represents a rational number in Smalltalk. I figured I would share my source code with the interwebs for anyone else trying to learn the language. I release the code under the GNU General Public License v3.

  • heap sort in ML ·

    This past semester for my programming languages class, we had to implement heap sort in Standard ML. This is my implementation, which I release under the GNU General Public License v3. I split the code into two separate files: shared.ml and heap_sort.ml, where shared.ml contains functions that might be of use with other sorting algorithms and heap_sort.ml contains functions and code specific to heap sort.

  • code reports Rake task ·

    This will run various Ruby Gems for creating code quality reports, store them in public/ in your Rails app, and create public/reports.html with links to each report. You need Saikuro, Flog, Flay, Reek, and Roodi, since this Rake task uses them all.

  • sortable arrays of symbols in Ruby ·

    I got this error in my Rails app because I was trying to sort an array of symbols: undefined method `<=>’ for :my_symbol:Symbol. I defined the spaceship method for the Symbol class and included the Comparable module in order to have other comparison methods available for symbols.

  • C++ shell with forks and pipes ·

    As an assignment for my operating systems class, we were to write a shell in C or C++. I’m putting my work here under the GNU General Public License v3 in hopes that it will be helpful for someone else, presumably some future student arguing with the C language, which I find infinitely frustrating to work with sometimes.

  • my Qbee PHP script ·

    I’m a member of the Quilting Bee and I use a very simple PHP script I wrote for displaying my quilt. I figure someone else might get some use out of it, so here it is for your coding pleasure:

  • chmodding and Ruby ·

    Recently, I switched from a Powerbook to a Macbook, and to copy my files from one to the other, I used a pen drive. Since my pen drive has a FAT file system, it treats everything as being executable. This, however, is not the case on a UNIX-like file system like OS X. In order to save myself the hassle of manually chmodding thousands of files, I wrote this Ruby script:

  • using Ruby to rename files and edit their content ·

    Recently at work, the web admin for the computer science department came into our lab and told us that my employer’s site was broken. The admin had need to make all .php files not act as PHP scripts, and instead, all files with the extension .sphp would now run as PHP scripts. Since my employer’s site was built using PHP, that meant all of its pages were showing the source code instead of actually executing. I had to whip up a quick Ruby script in order to:

  • finding invalid foreign keys in Rails ·

    Sometimes it would be useful to tell users of your Ruby on Rails application if there is a problem in the database, such as some foreign keys are invalid. As an example, let’s assume you have two models, Book and Author, such that each Book has an author_id which connects with Author via its primary key, id. That is, the tables are: books(id, author_id) and authors(id). Each table probably has fields other than that, but those are the only fields we need to worry about. Below is a method that generates an unordered HTML list for display to the users:

  • the many methods to #find things in Rails ·

    For the longest time, I didn’t understand the full power of the various #find methods in Rails. I probably still don’t, but my understanding of them has certainly expanded. I used to use plain #find for everything. If I wanted to find all rows in the table ‘groups’ that had an ‘id’ field value of 1, 2, 3, or 4, I would do something like this:

    1
    2
    3
    4
    
    Group.find(
      :all,
      :conditions => ["id=? OR id=? OR id=? OR ID=?", 1, 2, 3, 4]
    )
    
  • #post method in tests with a different controller ·

    I wanted a #login method in test_helper that would allow me to easily login from any of my functional tests. However, the #post method won’t allow you to set a different controller than the one in the @controller instance variable that’s defined in your test’s #setup. Well, by looking at how the #process method works, you can see that it just grabs the controller from @controller. Redefine that, and you’re good to go:

  • go from model to associated table name and back ·

    Given a table object, it returns the related string object; e.g. SubAttribute => 'sub-attribute'. Useful if you want to make a list of all your tables with perhaps their fields listed out to the side.

  • conditioner for ActiveRecord-friendly conditions from a collection ·

    I frequently have a collection of values that I want to match in an ActiveRecord query, but it would be nice if I could let ActiveRecord handle checking the data and escaping it properly. So, I wrote this method to return ActiveRecord-friendly conditions, such as:

    1
    
    ["user_id=? AND job_id=?", 3, 4]
    
  • measuring volume in C++ ·

    This is a program I wrote earlier while killing time in the Multilab.