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."

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

<%= "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

# okish
class FooError < StandardError; end

# good
FooError =

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"

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
  return total_taxes_owed

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

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

  def mv(), @new_path) end

  def touch() FileUtils.touch(@fpath) 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

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.

Creating empty classes as oneliners

I like to follow the community styleguide in ruby -> Style Guide

Particularly, I like the single-line class definitions rule.

It’s known that this looks terrible. No effort to clean up space:

class FooError < StandardError

Working with others, I see the ‘okish’ one-line version all of the time:

class SomeClass < RuntimeError; end

But I always give a silent nod to anyone that I see using the simpler choice:

FooError = 

Again, this is all from the community style guide -> Style Guide