Algorithm for Secret Santa

In my family, we have the tradition of doing Secret Santa for gift exchange, so I wrote a small program that randomly selects pairs of gifters and receivers and then sends an email to each of them informing who they have to give a gift to. The first iteration of this problem was very convoluted, it tried to use an array of people and randomly select two of them, then it performed a series of checks in order to obey to the game's constraints: [Read More]

Renaming and deleting the current file in Emacs

There are multiple ways to delete a file in Emacs, but, as far as I know, they all require you to leave or find the file you want to operate on. At some point I came across the tip I'll share today, but unfortunately, I don't remember where I found it. Not having to leave the current buffer really speed up the delete/rename process and, for that purpose, I have two custom commands in my config [Read More]

Writing a CLI in Ruby using the Trollop gem (follow up)

On our last post we learned to create a basic CLI application using Trollop. We learned how to parse command line options with different types and how to ask for help. Today I want to explore some more advanced options. Sub-commands A sub-command is a way of branching our code to perform different actions depending on a command string. A great example for this is the git CLI. Calling git status will perform something completely different than calling git branch or git clone. [Read More]

Writing a Ruby CLI using the Trollop gem

I don't write CLI applications often enough, but when I do, I like to use the Trollop gem. Here's a simple tutorial on how to use it. We'll go with the most basic usage on this post. A simple example Say we want to create a script to publish a blog post from the terminal (we'll just fake the actual work for this example). We need to pass some data in order to create it: The post title, which is a string A flag denoting if it's a draft The post ID, in case it exists on the server, which is an integer This is quite easy [Read More]

Converting ERB templates into Haml in Emacs

In my Ruby web projects I like to use Haml as a templating language. Sometimes I'm using some CSS/JS framework (such as Bootstrap or Foundation). When this is the case, I normally go to it's documentation, copy a snippet of code and paste into my templates. Then I need to convert it into Haml and here's where Emacs+Ruby come in. The gem Before copying any code in this article, you'll need to install the html2haml gem. [Read More]


Has this ever happened to you? You have a json structure that gets converted into a hash. It has a structure that's a variation on this: { 'product': { 'title': 'Light saber', 'type': 'weapon', 'price': { 'value': 1000, 'currency': 'Imperial credits' } } } Say we want to extract the value attribute from the last level of nesting, so we do: response[:product][:price][:value] # ~> NoMethodError: undefined method `[]' for nil:NilClass # => # ~> NoMethodError # ~> undefined method `[]' for nil:NilClass # ~> # ~> xmptmp-in2891u-0. [Read More]

Moving lines around in Emacs

A few days ago I showed you a command I wrote for duplicating the current line in Emacs. Today I'll show you one (well, two, actually) that usually accompany it: moving lines up and down. I normally use it to copy a line in another place in the current buffer, by first duplicating and then carrying it to it's final position. But there are other uses, for example, some times I write some code and find out that I messed up the order of operations. [Read More]

Using case statements vs. using a Hash for simple selections in Ruby

The other day I found this piece of code at work: case status when 'booked' MyNamespace::Success when 'cancelled', 'canceled' MyNamespace::Cancelled when 'pending' MyNamespace::Pending else MyNamespace::Unknown end and I remembered that in one of her talks, Sandi Metz used a Ruby Hash to select a class for a factory. Something like this: { 'booked' => MyNamespace::Success, 'cancelled' => MyNamespace::Cancelled, 'canceled' => MyNamespace::Cancelled, 'pending' => MyNamespace::Pending, }.fetch(status) { MyNamespace::Unknown } I guessed that the Hash would be a somewhat faster approach, so I decided to benchmark it. [Read More]

Duplicate line in emacs

Quite often I find myself wanting to duplicate the line I'm standing on in my Emacs buffer. Maybe I need all or part of the text repeated below, or just the general structure to modify in place. Sometimes I create a simple template for something that I need to fill out over and over. But duplicating a line in Emacs is not a trivial task. Here's a basic workflow for doing so: [Read More]

Ruby scratch buffer

Even though I do some programming in bash, JavaScript, elisp, etc, at the end of the day, my go-to language is Ruby. When I need to do a quick and dirty script, calculation or every time I need to understand something about the language or gems, I sometimes create a Ruby file and operate there, using Ruby, rcodetools and/or seeing_is_believing (more n those in a future post). But Emacs has a very useful " [Read More]