• Arel and tags ·

    I love working out ActiveRecord and Arel queries in Rails apps. I thought I’d share some of the more fun queries I’ve designed in Arel, spread out over a few blog posts. We’ll start with one for fetching tags separated by account.

  • Steamy Screenshots ·

    My latest project is Steamy Screenshots (view source). It’s an app I made to extract colors from the screenshots people share on Steam. There’s no real usefulness to it, it was just a fun thing to do. You can look up a particular Steam user and view their newest fifty screenshots, or look at the newest screenshots shared for a particular game.

  • BlicblockJS: a game from The Sims 4 ·

    I made another thing! This time, I made my first game. Well, the first game I’ve ever finished (I’ll get back to you someday Mahjong). It’s a JavaScript web implementation of the game Blicblock that your Sims play in The Sims 4. I built it using AngularJS, my new loves Yeoman and Bower, and Bootstrap. You can try it out in your web browser and view or contribute to the source code on Github.

  • 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.

  • Switching from WordPress to Jekyll ·

    Over the course of a little less than a week, I migrated this blog from WordPress 3.8 to Jekyll. I used WordPress for years, probably since 2005 or 2006 when I switched away from Movable Type. I finally decided I wanted something more lightweight than WordPress, and in particular I wanted a blog that would load faster. Jekyll gives me both of those. The part that took the longest to migrate was customizing the new layout, based on Dbyll, and formatting the code examples in my posts.

  • RailsBridge Lexington ·

    Yesterday I helped out at the first ever RailsBridge held in Lexington. We had twenty-five people sign up as attendees and maybe ten volunteer coaches, including myself. It’s intended to be an all-day session introducing women to programming, specifically by having them create a simple Ruby on Rails app. I gave the opening presentation, which you can find on SlideShare and also download as a PDF. It introduced the ladies to programming basics, and had them walk through several examples in IRB. Friday night we held an InstallFest where everyone brought their laptops and we helped them get stuff installed, including RailsInstaller in Windows, Git, Sublime Text 2 as an editor, and Node.js in Windows to have a JavaScript runtime. They also made a couple basic Rails apps that were no more than scaffolds, committed them to local Git repositories, and published them on Heroku. This was to prepare them for Saturday, when they did the same thing but with a more complex Rails app.

  • Solr and Paper Mario on the 3DS ·

    My new job at CirrusMio has been going great these past couple of months. I got to go to RubyConf 2012 in Denver, which was awesome. I had never been to RubyConf before, and it was great to go to a conference so relevant to what I do and enjoy. I especially enjoyed Refactoring from Good to Great and Jim Weirich’s keynote.

  • 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:

  • 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.

  • 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:

      :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:

    ["user_id=? AND job_id=?", 3, 4]
  • simple Rails preference storage ·

    So you’ve got some Rails application and you need to store information from the users across their interactions with the app. Here’s a simple, straightforward way to do that.

  • if succinctly ·

    In Ruby, if is an expression, not a statement, thus it returns a value. This may not seem useful at first glance, but it lends itself to forming neat, concise code… Like most things in Ruby, actually.

  • the power of yield and super ·

    The two keywords yield and super allow you to pass control back and forth between parent and child methods, to weave power between a more general method (in the parent class) and a more specific method (in the child class) with ease and logic. Using yield and super effectively can help you maintain the DRY (Don’t repeat yourself) principle, keeping your code easier to maintain.