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.

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
end

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 = Class.new(StandardError) 

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

Split Strings into chars ruby

Splitting strings into chars is a common programming application, especially if you’re parsing a lot of text or reading line by line. Below is a couple examples from the other day I used to split strings in ruby.

file_lines = File.readlines(File.join(__dir__, "/post.md"))

# parse H1 in md
file_lines.each do |line|
  line.chars[0] == "#" ? (format_h1) : (nil)
end

# remove H1 leading '#'
def remove_prefix(line)
  line.shift
end

# Pop the last char
def remove_last_char(line)
  line.pop
end

Nothing fancy, but useful if you were parsing markdown manually like I was. TC