## Hash#transform_keys

Ruby 2.5 introduced several new methods. Everyone is happy for yield_self (don't get my wrong, me too), but I'm very excited for one I've been waiting for: Hash#transform_keys. It does exactly what it name implies, it provides a way to transform each key on the Hash. What's the use for this? well, let's explore one of them. Say we're writing a method that takes a Hash and returns a Json string representing that hash for an API. [Read More]

## Make parent directory if it doesn't exist when saving a file in Emacs

Sometimes I create a buffer by navigating into a directory that doesn't exist. For example, I create a brand new project (an empty dir), then navigate into <project_dir>/lib/<project_name>.rb. Emacs happily allows me to do this because buffers don't necessarily need to be bound to an actual file, so no problem there. The problem comes when I try to save. As the lib directory doesn't exist, I get an error. The solution The way to solve this is to write an elisp function to create the (in this case) lib directory. [Read More]

## 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]

## Hash#dig

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]