Starting Gem builds with 'bundle init'

I’ve been building a lot of tiny gems lately to script out some basic stuff. It’s super easy, super powerful, and a lot of fun. I thought I’d share a few tips and tricks to get you rolling out your gem init.

The basics:

Building a gem is simple. All your need to do is add a .gemspec file and file to call your methods from. But how do you grow a bundle?

bundle gem <gem_name>

This is going to spit out a nice little dir:

bin/
lib/
test/
your_gem.gemspec
Gemfile
Rakefile
...

Then… get to it. Having a dir structure is super important to me personally, so this gives me a big head start. I’m also a fan of cleaning the entire dir out if the script is only one file. But that’s for another time.

Ruby shorthand statements

Ruby has a few ways to write shorthand if/else and control flow statements. I’ve really come to like JS’s shorthand statements, and have since adopted oneliners in ruby wherever I can. Just putting this code here for reference later.

# Bad
if 1 > 0
  p "Hello World."
end

# Good
p "Hello World" if 1 > 0


# Good, with nil else condition
1 > 0 ? (p "Hello World") : nil

# Good, with else condition
1 > 0 ? (p "Hello World") : (p )

# ERB
<%= "Hello World" if 1 > 0 %>

Only gotcha is wrapping the conditional functions in parentheses.

Ruby has a pretty Hello World program

As I’ve begun studying C++ and other systems languages, I’ve begun to appreciate ruby for it’s beauty. Learning ruby as a first lang has always been a slight regret of mine, mainly because I lived in the “sauce” of ruby for too long. Instead of approaching problems at the lowest level possible (closer to ultimate truth), I was usually in some high-level abstraction in Rails.

One thing that I find just darn pretty is the Hello World comparison between the two. Obviously, this is just a syntax comparison, but dang! If this were about performance we wouldn’t even be talking about Ruby.

Hello world in C++:

#include <iostream>
int main()
{
  std::cout << "Hello World!";
}

and in Ruby:

p "Hello World!"

Creating empty classes as oneliners

I like to follow the community styleguide in ruby -> Style Guide Prefer a single-line format for class definitions with no body. [link]

# bad
class FooError < StandardError
end

# okish
class FooError < StandardError; end

# good
FooError = Class.new(StandardError)

Assigning Conditionals to Variables

One pattern I’ve been using a lot lately is assigning conditionals to variables in Rails. It’s particularly useful when I’m translating a numerical identifier into it’s human readable param.

Like parsing one of five difficulties in a game:

def set_difficulty
  difficulty = case params['difficulty']
               when 0 then 'very_easy'
               when 1 then 'easy'
               when 2 then 'medium'
               when 3 then 'hard'
               when 4 then 'impossible'
               else raise "Invalid Difficulty"
               end
 end

I just think that’s a really elegant, readable pattern. Another good example is having two calculations, varying on input.

def file_taxes(filing_date, deadline, data)
  total_taxes_owed = if filing_date > deadline
                        file_late_taxes(data)
                      else
                        file_late_taxes(data)
                      end
  return total_taxes_owed
end

Again - it makes it simple to pass around the result of the conditional, while also being easy to read.

One line methods ruby

One liners are key in any programming language. I’m come to like this syntax in ruby most:

def hello_world() p "hello world" end

Some people think that the lack of comma makes it hard to read, and I don’t really agree. The parentheses are a nice visual queue for me, so I roll with that.

Here’s a better example:

class FileManager
  def init(path, *new_path)
    @fpath = AGRV[1]
    @new_path = new_path if new_path
  end

  def rm() FileUtils.rm(@fpath) end

  def mv() FileUtils.mv(@fpath, @new_path) end

  def touch() FileUtils.touch(@fpath) end
end

I think those single like methods look easy to read, and give a slick “ruby-like” swag to them.

I use the Ruby style guide -> Ruby Style Guide

FileUtils -- #touch

Was on the FileUtils docs the other day, found a neat little ‘shell-like’ method in ruby.

# Create a blank .vimrc
FileUtils.touch('/Users/igolden/.vimrc')

Could come in handy for scripting personal setups, provisioning on a server, or setting up a Docker container. Not something I’ll use often, but cool to know more ruby secrets.