Skip to content

Instantly share code, notes, and snippets.

View midwire's full-sized avatar
🏠
Working from home

Chris Blackburn midwire

🏠
Working from home
View GitHub Profile
@midwire
midwire / install_ruby
Created July 30, 2014 15:01
Script for ruby-install
#!/usr/bin/env bash
# Script for ruby-install
build=`which ruby-install`
rubies="$HOME/.rubies"
if [[ ! -x $build ]]; then
echo "'brew install ruby-install' before using install_ruby"
exit 1
fi
@midwire
midwire / cherry_pick_external.rb
Last active August 29, 2015 14:05
Cherry Pick commits from an external repository
#!/usr/bin/env ruby
require 'pry'
require 'colored'
require 'trollop'
class CherryPickExternal
attr_accessor :external_repo, :sha, :elapsed, :color_output, :test_run
def self.run(*args)
start_time = Time.now
@midwire
midwire / hash.rb
Created August 19, 2014 01:10
Better Ruby Hash
class Hash
def grep(pattern)
inject([]) do |res, kv|
res << kv if kv[0] =~ pattern or kv[1] =~ pattern
res
end
end
# Usage { :a => 1, :b => 2, :c => 3}.except(:a) -> { :b => 2, :c => 3}
def except(*keys)
@midwire
midwire / array.rb
Created August 19, 2014 01:12
Better Ruby Array
class Array
def count_occurrences
k = Hash.new(0)
self.each{|x| k[x] += 1}
k
end
def randomize
self.sort_by { rand }
end
@midwire
midwire / stat.rb
Created August 19, 2014 01:14
Get the device name where a file resides
class File
class Stat
def self.device_name(file)
Dir['/dev/*'].inject({}) { |h, v|
h.update(File.stat(v).rdev => v)
}.values_at(File.stat(file).dev).first || nil
end
end
@midwire
midwire / fixnum.rb
Created August 19, 2014 01:17
Enhanced Ruby Fixnum class
class Fixnum
# Format a number with commas and a decimal point
def commify
to_s =~ /([^\.]*)(\..*)?/
int, dec = $1.reverse, $2 ? $2 : ""
while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3')
end
int.reverse + dec
end
@midwire
midwire / rake_helper.rb
Created August 19, 2014 01:18
Automate gem releasing and versioning
require 'thor'
require 'midwire_common'
module MidwireCommon
# RakeHelper helps to automate gem release and versioning tasks
class RakeHelper
include Rake::DSL if defined? Rake::DSL
def self.install_tasks(opts = {})
@midwire
midwire / version.rake
Created August 19, 2014 01:20
Gem or project semantic versioning rake tasks
require 'rubygems'
require 'rake'
require 'readline'
require 'fileutils'
require 'pry'
module Bundler
class GemHelper
protected
@midwire
midwire / time_tool.rb
Created August 19, 2014 01:21
Convert time (HH:MM:SS) to seconds and back again
module MidwireCommon
class TimeTool
# converts the given time (HH:MM:SS) to seconds
#
# +time+ the time-string
def self.time_to_seconds(time)
return -1 if time.nil? or time.strip.empty?
times = time.split(/:/).reverse
@midwire
midwire / time.rb
Created August 19, 2014 01:22
I always seem to need this method to get a proper timestamp for files and such.
class Time
class << self
def timestamp
"#{Time.now.strftime("%Y%m%d%k%M%S")}"
end
end
end