Category Archives: ruby

Render a template from a hash in Ruby

A hash is a great way to manage a list of key/value pairs, and templates are a great way to render content. So here’s a simple way to render a template from a hash. template.erb My favorite color is <%= … Continue reading

Posted in ruby | Tagged , , , , , , , | 1 Comment

extconf.rb:4:in `require’: no such file to load — mkmf (LoadError)

I’ve hit this a dozen times and I always forget the solution. When you get this on Ubuntu, sudo apt-get install ruby-dev

Posted in ruby | Tagged , , , , , , , | 1 Comment

Show Duplicate Filenames with Ruby

Here’s a quick one. I want to show just the names of files that have the same basename in a directory. Code require ‘find’ require ‘set’ all_files  = [] duplicates = {} basenames  = Set.new ARGV.each do |dir|   Find.find … Continue reading

Posted in ruby | Tagged , , , | Leave a comment

Ruby instance_eval gotcha

I don’t know how much of a “gotcha” this is since it’s just a defined behavior and feature of the language, but I still found it a little counterintuitive when I encountered it, and I had to actually read the … Continue reading

Posted in ruby | Tagged , , , , | 1 Comment

Run Sinatra App in a Subdirectory with Rack::Builder.map

Thank god I found Tanner Burson’s blog. I was about to explode. I recommend reading Tanner’s original post, but the short answer is that you want Rack to do some routing for you via Rack::Bundler.map. Here is a super simple … Continue reading

Posted in Sinatra | Tagged , , , , , , , , , , , , , , , | Leave a comment

Sanitizing File Names with Ruby

At work, I ran into a problem wherein I was copying files from one filesystem (FAT32) to another (NTFS), and there were file names with characters that were legal in the first, but not the second. I wrote a Ruby … Continue reading

Posted in ruby, scripts | Tagged , , , , , | Leave a comment