Skip to content

Instantly share code, notes, and snippets.

View zerowidth's full-sized avatar

Nathan Witmer zerowidth

View GitHub Profile
@zerowidth
zerowidth / example.rb
Created July 28, 2011 18:41
short circuiting and side effects
# as suggested by @mcmire, https://twitter.com/mcmire/status/96632723220869120
# and clarified in https://twitter.com/mcmire/status/96647536521129985
class Array
def thoroughly_all?(&block)
all_true = true
each {|obj| all_true &= yield(obj) }
all_true
end
@zerowidth
zerowidth / copy_rtf.vim
Created July 21, 2011 01:50
copy a vim buffer or selection as syntax-highlighted RTF to the clipboard
" copy the entire buffer or selected text as RTF
" inspired by https://github.com/dharanasoft/rtf-highlight
" but only uses commands available by default on OS X.
"
" To set html conversion options, :help TOhtml
" And, undocumented, to set the font used,
" let g:html_font="Your Preferred Font"
"
function! CopyRTF(line1,line2)
if !executable('textutil')
@zerowidth
zerowidth / orequals.rb
Created May 29, 2011 15:43
a demonstration of the ||= idiom
#!/usr/bin/env ruby -wKU
#
# a demonstration of the ||= idiom
#
# x ||= y
#
# is usually translated as
#
# x = x || y
#
@zerowidth
zerowidth / api_hacks.rb
Created May 26, 2011 03:16
quick api hacks
# requires patron, nokogiri, map
class Api
Error = Class.new(StandardError)
class << self
attr_accessor :base_url
end
attr_reader :http
Rubinius Crash Report #rbxcrashreport
Error: signal SIGSEGV
[[Backtrace]]
0 rbx 0x000000010000fb30 _ZN8rubiniusL12segv_handlerEi + 160
1 libSystem.B.dylib 0x00007fff88ee366a _sigtramp + 26
2 ??? 0x0000000000000643 0x0 + 1603
3 rbx 0x0000000100019beb _ZN8rubinius11InlineCache11empty_cacheEPNS_2VMEPS0_PNS_9CallFrameERNS_9ArgumentsE + 91
4 ??? 0x0000000102c7df92 0x0 + 4341620626
@zerowidth
zerowidth / truncate_in_place.rb
Created May 23, 2011 23:20
test out truncating a file in place
#!/usr/bin/env ruby
log = "big.log"
words = %w(foo bar baz blech mumble what no ack aiee)
File.open(log, 'w') do |file|
1000.times do |n|
content = (0..(rand(100) + 10)).map { words[rand(words.size)] }
file.puts "#{n} #{content.join ' '}"
end
Dreamer [Fischer], level [1], priority [19], policy [OTHER]
Dreamer [Cobb], level [1], priority [19], policy [OTHER]
Dreamer [Ariadne], level [1], priority [19], policy [OTHER]
Dreamer [Arthur], level [1], priority [19], policy [OTHER]
Dreamer [Eames], level [1], priority [19], policy [OTHER]
Dreamer [Yusuf], level [1], priority [19], policy [OTHER]
Dreamer [Saito], level [1], priority [19], policy [OTHER]
[Fischer] HIJACKED ! Open up my defense projections in my dream to the hijackers!
[Cobb] sees Fischers defense projections at work in the dream at level [1]
[Cobb] taking Fischer to level 2
@zerowidth
zerowidth / Gemfile
Created February 24, 2011 03:00
comparison of mongo's grep vs. pcregrep on 400+MB of log files
source :rubygems
gem "bson_ext"
gem "mongo"
gem "logging"
@zerowidth
zerowidth / trollop_modes.rb
Created January 19, 2011 19:18
hacky test at declarative mode / subcommand support in trollop, totally unfit for consumption!
require "rubygems"
require "trollop"
Trollop::Parser.module_eval do
def self.mode_parse(args=ARGV, &block)
parser = Parser.new(args, &block)
with_standard_exception_handling(parser) { parser.mode_parse args }
end
@zerowidth
zerowidth / ssh_sudo_root.sh
Created January 6, 2011 21:20
sshr for "ssh as root", but using sudo
function sshr() {
if [ -n "$1" ]; then
if [ -n "$2" ]; then
ssh -t $1 sudo $2
else
ssh -t $1 sudo su -
fi
else
echo "... specify a host, n00b"
fi