Arel and tags · Jun 19, 2015
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 · May 8, 2015
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 · Oct 1, 2014
VCR for shell scripts · Jan 24, 2014
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-cmdscript for importing an instance into Amazon.
Switching from WordPress to Jekyll · Jan 21, 2014
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 · Apr 21, 2013
Solr and Paper Mario on the 3DS · Dec 6, 2012
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 · Oct 20, 2010
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
Vectorclasses that I found helpful.
convenient file searching with Ruby, grep, and file · Feb 23, 2010
For my Linux kernel class, I often know that some
structexists 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 · Apr 7, 2009
sortable arrays of symbols in Ruby · Apr 7, 2009
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 · Feb 1, 2008
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 · Nov 21, 2007
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 · Nov 15, 2007
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 · Nov 22, 2006
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:
#post method in tests with a different controller · Nov 14, 2006
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 · Aug 31, 2006
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 · Aug 31, 2006
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 · Aug 11, 2006
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 · Jun 7, 2006
ifis 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 · May 28, 2006
The two keywords
superallow 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
supereffectively can help you maintain the DRY (Don’t repeat yourself) principle, keeping your code easier to maintain.