Skip to content

Instantly share code, notes, and snippets.

@phlipper
phlipper / ubuntu_ami_hashes.rb
Last active January 3, 2016 21:59
Hacky snippet to get current Ubuntu AMI information as an Array of Ruby Hashes
require "json"
require "open-uri"
url = "http://cloud-images.ubuntu.com/locator/ec2/releasesTable"
json_string = open(url).read
json_string.slice!(-6) # remove trailing comma, invalid json :\
collection = JSON.load(json_string)["aaData"].inject([]) do |arr, row|
# ["us-west-2", "raring", "13.04", "i386", "instance-store", "20130706", "<a href=\"https://console.aws.amazon.com/ec2/home?region=us-west-2#launchAmi=ami-658e1d55\">ami-658e1d55</a>", "aki-fa37baca"]
@phlipper
phlipper / ruby.rb
Created November 22, 2013 17:08
quick chruby/ruby-install chef recipe
# ruby-install
ruby_install_version = node["ruby_install"]["version"]
ruby_install_url = "https://github.com/postmodern/ruby-install/archive/v#{ruby_install_version}.tar.gz"
ruby_install_dir = "ruby-install-#{ruby_install_version}"
ruby_install_tar = "#{ruby_install_dir}.tar.gz"
execute "Install ruby-install" do
cwd "/tmp"
command <<-EOC
curl -Lo #{ruby_install_tar} #{ruby_install_url} &&
@phlipper
phlipper / .tmux.conf
Created September 2, 2013 23:56
~/.tmux.conf scratch files
# use capslock+a for the prefix key
set -g prefix C-a
# unbind initial prefix key to use later
unbind C-b
# more responsive default delay for vim
set -sg escape-time 1
# tmux-pasteboard
class ExponentialBackoff
def initialize(initial, max)
@initial = initial
@max = max
@current = initial
@step = 0
end
def reset
@phlipper
phlipper / app_models_collateral.rb
Created October 11, 2012 17:39
Example Paperclip Interpolation
class Collateral < AR::B
has_attached_file :movie, url: "/uploads/:attachment/:id/:cache_key/:filename"
end
@phlipper
phlipper / gist:3798682
Created September 28, 2012 08:40
FactoryGirl syntax sugar for MiniTest

minitest is a great testing library and factory_girl is a handy fixture replace library, especially when working with Rails.

It can get tedious to repeat FactoryGirl. for every invocation of build, create, etcetera. Here is a handy shortcut to help reduce your Carpal Tunnel pain:

# for Test::Unit assertion style
class MiniTest::Rails::ActiveSupport::TestCase
  include FactoryGirl::Syntax::Methods
end
@phlipper
phlipper / Gemfile
Created August 3, 2012 22:38
Examples from Coderwall Protip: Install PostgreSQL 9.2 on OS X Mountain Lion
source :rubygems
gem "rails"
gem "pg"
# ...
@phlipper
phlipper / multiple_splats.rb
Created July 21, 2012 19:05
Flatten multiple arrays at once using multiple splats with Ruby 1.9
first = [1, 2, 3]
# => [1, 2, 3]
middle = [4, 5, 6]
# => [4, 5, 6]
last = [7, 8, 9]
# => [7, 8, 9]
all = [*first, *middle, *last]
@phlipper
phlipper / splat_arg.rb
Created July 21, 2012 18:56
Splat argument before required argument in Ruby 1.9
def foo(a, *b, c)
puts [a, b, c].inspect
end
foo(1, 2, 3, 4, 5)
# => [1, [2, 3, 4], 5]