Ruby CLI Shell Scripting Cheatsheet

Last updated:

installing (on ubuntu)

sudo apt-get install ruby1.9.1-full


this is required at the start of every ruby script

#!/usr/bin/env ruby

indented heredocs

useful to display messages on the CLI.

6 is used as paremeter to the gsub call because that's the amount of spaces prior to the text. (and will be stripped off the text).

text = <<-END.gsub(/^ {6}/, '')
      "Faith" is a fine invention
      When Gentlemen can see
      But Microscopes are prudent
      In an Emergency.
      (Emily Dickinson 1830-1886)

source: ruby quicktips


add this to the beginning of your script ( on the line directly below the hashbang! it can't be anywhere else or it won't work!)

# encoding: UTF-8

create a file"path/to/file","a+"){|handle|
    #do stuff to the file like write text

w.i.p.: this is a work in progress. Expect more content to be added.

Dialogue & Discussion