Skip to content

Instantly share code, notes, and snippets.

set history=256
set autowrite
set autoread
set timeoutlen=250
set clipboard+=unnamed
set directory=~/.vim/swap
set hlsearch
set incsearch
@wbailey
wbailey / negative index
Created December 21, 2010 07:55
A trick using a negative array index
1.upto(10) {|i| puts %w{current after before}[i <=> 5]}
@wbailey
wbailey / objectspace.rb
Created December 21, 2010 08:01
Exploring the object space of a ruby class
ruby-1.9.2-p0 > a = []; ObjectSpace.each_object(Kata) {|o| a << o.class if o.respond_to? 'gets'}
=> 20576
ruby-1.9.2-p0 > a
=> [IRB::Locale, File, ARGF.class, IO, IO, IO, Module, RubyLex, IO, IO, IRB::ReadlineInputMethod]
@wbailey
wbailey / github_user.rb
Created December 22, 2010 20:17
A trick using a closure for memoization
def github_user
@github_user ||= begin
github_user, shell_user = %x{git config --get github.user}.chomp, ENV['USER'].to_s
raise Exception, 'Unable to determine github user' if (github_user + shell_user).empty?
github_user.empty? ? shell_user : github_user
end
end
@wbailey
wbailey / github_user_1.rb
Created December 23, 2010 18:20
A first pass at the github_user method
We couldn’t find that file to show.
@wbailey
wbailey / files1.rb
Created December 28, 2010 08:21
Looping through files in a poor way
Dir['*'].each do |file|
gzip = false
if file.match(/\.gz$/)
use_file = file.sub(/\.gz$/)
gzip = true
else
use_file = file
end
@wbailey
wbailey / files2.rb
Created December 28, 2010 08:27
Using a mixin to extend the object instance with the functionality we want
module Logfiles
def name
self.sub(/\.gz$/)
end
def skip?
!(@options[:unzip] && self.gzip?)
end
def gzip?
@wbailey
wbailey / class_string_introspection.rb
Created January 14, 2011 21:50
Using a class to define methods that examine the contents of a string
class MyString
attr_accessor :str
def initialize str
self.str = str
end
def consonants
str.gsub /[aeiuo0-9]/i, ''
end
@wbailey
wbailey / string_benchmark.rb
Created January 14, 2011 23:50
Testing various implementations of introspecting on a string
require 'benchmark'
module MyShortcuts
def consonants
self.gsub /[aeiuo0-9]/i, ''
end
def vowels
self.gsub /[^aieou]/i, ''
end
@wbailey
wbailey / string_introspection_mixin.rb
Created January 14, 2011 23:15
Using a mixin to extend the object and use it's eigenclass
module MyString
def consonants
self.gsub /[aeiuo0-9]/i, ''
end
def vowels
self.gsub /[^aieou]/i, ''
end
def capitols