Skip to content

Instantly share code, notes, and snippets.

View shadoi's full-sized avatar

Blake Barnett shadoi

  • Apple
  • San Francisco
View GitHub Profile
def self.included(base)
base.extend(ClassMethods)
end
# Returns a hash of methods organized by their owner and method type.
module Kernel
def methods_by_owner
output = {}
output[:private_methods] = {}
output[:instance_methods] = {}
output[:protected_methods] = {}
require "yajl"
parser = Yajl::Parser.new
encoder = Yajl::Encoder.new
regex = /(.*)asdf(\w+)\s+{3}/
regex_json = encoder.encode(regex)
r = Regexp.new(parser.parse(regex_json))
=> /(?-mix:.*asdf\w+\s+{3})/
"fooasdfblah argasdfbuh wowasdfderp" =~ r
@shadoi
shadoi / gist:1070375
Created July 7, 2011 19:47
Is this string a number?
# Strip leading zeroes, check if input matches after conversion to integer
class String
def is_num?
self.to_i.to_s == self.gsub(/^0+/, '')
end
end
> "045".is_num?
=> true
module A
def some_instance_method
"hi"
end
module ClassMethods
def foo
"asdf"
end
end
class Test
def initialize
@atts = Hash.new
end
def prop=(value)
@atts[:prop] = value
end
def prop
if days_to_wait == 1
puts "."
else
puts "s."
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}
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
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|