## Executing a command in a VirtualBox VM from the outside

At work, I regularly have to run scripts, tests or commands on a Virtual Machine. To do this, the common approach is to log in via SSH and then just run what I need to run. But sometimes, there are commands that need to be run repeatedly. VirtualBox allows us to do it from outside the machine by using vagrant ssh -c '<command>' <vm_name> which is a huge help. [Read More]

## 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. [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 File: =fdx/rename-current-buffer-file This command will try to rename the current buffer. [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. [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 require 'trollop' require 'awesome_print' opts = Trollop::options do opt :title, "Post title", type: :string opt :draft, "Create as a draft", short: :r opt :post_id, "Post id", type: :integer end ap opts When we run this, we get: \$ ruby post --title="Trollop" -r true { :title => "Trollop", :draft => true, :post_id => nil, :help => false, :title_given => true, :draft_given => true } So, we have a Hash extracted from the ARGV Array. [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]