Skip to content

Instantly share code, notes, and snippets.

@acook
acook / ex_proc.rb
Created February 29, 2012 22:00
Wrapper for external processes
def run(*args)
output = Struct.new(:pid, :stdout, :stderr)
status = Open4.popen4(*args) do |pid, stdin, stdout, stderr|
output = output.new pid, stdout.read, stderr.read
end
[status, output]
end
@acook
acook / subsets.rb
Created March 7, 2012 20:40
subset benchmarks
require 'benchmark'
require 'set'
class Array
def subset_subtr?(a)
(self - a).length == 0
end
def subset_inter?(a)
(self & a).length == length
end
@acook
acook / gist:2040359
Created March 14, 2012 23:25
Adding submodules that already have their own repo
function git_add_submodule { git submodule add `cd $1; git remote -v | head -1 | cut -f 2 | cut -f 1 --delimiter " "` $1; }
@acook
acook / show_ancestors.rb
Created April 17, 2012 17:07
show the actual ruby ancestor hierarchy
def show_ancestors object
klass = object.is_a?(Class) ? object : object.class
klass.ancestors.map do |a|
a.is_a?(Class) ? a : a.ancestors
end
end
@acook
acook / example_output.txt
Created August 17, 2012 17:16
Recursive mass require files with only a relative path. Just require the `require_path_method.rb` file or copy/paste the contents into your project to use it. The `oneliner.rb` script performs identically to the full `requirepath.rb` script.
$ pwd
/Users/acook/Projects/personal/3380730
$ bash makepaths.sh
-- current_file: requirepath.rb
-- relative_path_of_current_file: .
-- path: /Users/acook/Projects/personal/3380730
-- filematch: /Users/acook/Projects/personal/3380730/features/support/**/*.rb
-- file: /Users/acook/Projects/personal/3380730/features/support/whatever/something.rb
-- required: true
@acook
acook / termsize.rb
Created December 2, 2012 17:31
Getting the terminal size in Ruby
# via http://www.megasolutions.net/ruby/Getting-the-size-of-the-terminal-in-a-portable-way-26006.aspx
TIOCGWINSZ = 0x40087468
def get_winsize
str = [0, 0, 0, 0].pack('SSSS')
if STDIN.ioctl(TIOCGWINSZ, str) >= 0
rows, cols, xpixels, ypixels = str.unpack("SSSS")
p rows, cols, xpixels, ypixels
else
puts "Unable to get window size"
@acook
acook / keypress.rb
Created December 2, 2012 18:42
Read keypresses from user in terminal, including arrow keys using pure Ruby. This has since been folded into a much more robust gem called Remedy. https://rubygems.org/gems/remedy & https://github.com/acook/remedy
require 'io/console'
# Reads keypresses from the user including 2 and 3 escape character sequences.
def read_char
STDIN.echo = false
STDIN.raw!
input = STDIN.getc.chr
if input == "\e" then
input << STDIN.read_nonblock(3) rescue nil
@acook
acook / gist:4199144
Created December 3, 2012 23:49
Git aliases
co = checkout
st = status
ci = commit
w = whatchanged
nb = checkout -b
b = branch
amend = commit --amend
unstash = stash pop
unstage = reset HEAD --
remotes = remote -v
@acook
acook / invert_array.rb
Created December 11, 2012 07:36
Invert an Array
def invert original
result = Marshal.load Marshal.dump(original)
original.each_with_index do |row, r_index|
row.each_with_index do |column, c_index|
result[c_index][r_index] = column
end
end
result
def extract_state object
state = { local_variables: {}, instance_variables: {} }
object_binding = object.send :binding
puts eval('local_variables', object_binding)
eval('local_variables', object_binding).each do |variable|
state[:local_variables][variable] = eval "#{variable}", object_binding
end
eval('instance_variables', object_binding).each do |variable|