head.daveops.net

Snippets for yer computer needs

Ruby

CLI

# Print warnings
ruby -w ...

# get machine instructions
ruby --dump insns script.rb

# See how commands are parsed
ruby --dump parsetree_with_comment script.rb

Abort on thread errors

Thread.abort_on_exception = true

Thread.new do
  fail 'Cannot continue'
end

loop do
  sleep
end

Syntax cheatsheet

class Thing
  attr_accessort :foo
  def initialize(foo = 0)
    @foo = foo
  end
  
  private
  
  def bar
    puts "whoa"
  end
end

if val = 42
  #do stuff
elsif val = 33
  #do stuff
else
  #do stuff
end

# Ternary operator
exp ? true : false

# Begin block
begin
  # try to do this
rescue Exception
  # oh crap
else
  # whatever else
ensure
  # do this no matter what
end

# Case statement
case thing
when 3
  puts 'fizz'
when 5
  puts 'buzz'
else
  puts thing
end

# Safe navigation operator (introduced in Ruby 2.3)
u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
# versus
u&.profile&.thumbnails&.large

Running proceses

% Notation

Modifier Meaning
%i[ ] Non-interpolated symbol array (Ruby 2+)
%I[ ] Interpolated symbol array (Ruby 2+)
%q[ ] Non-interpolated String (except for \ [ and ])
%Q[ ] Interpolated String (default)
%r[ ] Interpolated Regexp (flags can appear after the closing delimiter)
%s[ ] Non-interpolated Symbol
%w[ ] Non-interpolated Array of words, separated by whitespace
%W[ ] Interpolated Array of words, separated by whitespace
%x[ ] Interpolated shell command

Freezing strings

The frozen_string_literal pragma will have all strings in the code be immutable, improving performance. To debug, use --debug=frozen-string-literal

# frozen_string_literal: true