Skip to content

Instantly share code, notes, and snippets.

View shadoi's full-sized avatar

Blake Barnett shadoi

  • Apple
  • San Francisco
View GitHub Profile
correct_order = [ 'g', 'c', 'b', 'e', 'f', 'd', 'b' ]
actual_order = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ]
actual_order.sort_by! {|x| correct_order.include?(x) ? correct_order.index(x) : actual_order.length}
---
:benchmark: false
:verbose: true
gem: --no-ri --no-rdoc
:update_sources: true
:sources:
- http://rubygems.org
:backtrace: false
:bulk_threshold: 1000
rdoc: --inline-source --line-numbers --format=html --template=hanna
module Serialize
def to_h
h = {}
# My classes have an "attributes method that's created via JSON::Schema
attrs = self.class.attributes
attrs.each {|attr| h.store(attr, self.send(attr))}
h.delete_if {|k,v| v.nil?}
return h.symbolize_keys
end
set current_track to the current track
set track_album to the album of the current track
set track_artist to the artist of the current track
set track_comments to the comment of the current track
set track_name to the name of the current track
@shadoi
shadoi / mylogger.rb
Created March 26, 2012 22:45
Logger mixin
require 'logger'
module MyLogger
module ClassMethods
def log(msg=nil)
# Approximate syslog format
logger = Logger.new("my.log")
logger.formatter = proc { |severity, datetime, progname, msg|
progname ||= "my-log"
datetime_format = "%b %d %H:%M:%S"
class Foo
attr_accessor :blah
def initialize(options={})
options.each {|k,v| self.__send__("#{k}=", v) if self.respond_to?(k.to_sym)}
end
end
foo = Foo.new(:blah => "test", :blargh => "stuff")
config = {}
config[:security_groups] = ["default"]
config[:puppet_params] = {}
opt_parser = OptionParser.new do |opts|
opts.on("-c", "=CLASS", "") {|val| classes.push(val)}
opts.on("-p", /[a-zA-Z0-9_-]+=.*/,"=PARAMETER=VALUE",
"Set puppet variable PARAMETER to VALUE") do |val|
params = {}
security_group = {}
opt_parser = OptionParser.new do |opts|
opts.on("-c", "=CLASS", "") {|val| classes.push(val)}
opts.on("-p", /[a-zA-Z0-9_-]+=.*/,"=PARAMETER=VALUE",
"Set puppet variable PARAMETER to VALUE") do |val|
a = val.split('=',2)
params[a[0]] = a[1]
end
@shadoi
shadoi / gist:1346342
Created November 7, 2011 22:02
Totally pointless file handling example to show how I'd use exceptions with retry and ensure
class NotExecutable < RuntimeError; end
begin
f = File.open("test")
raise NotExecutable unless File.executable?(f)
rescue Errno::ENOENT => e
puts e.inspect
%x{touch test}
retry
rescue NotExecutable => e
%x{chmod +x test}
if days_to_wait == 1
puts "."
else
puts "s."
end