Skip to content

Instantly share code, notes, and snippets.

View knzconnor's full-sized avatar

Kenzi Connor knzconnor

View GitHub Profile

Keybase proof

I hereby claim:

  • I am timocratic on github.
  • I am timocratic (https://keybase.io/timocratic) on keybase.
  • I have a public key whose fingerprint is 7E07 1867 9F3F 2174 FA58 27C5 B405 8899 5CD3 22B9

To claim this, I am signing this object:

@knzconnor
knzconnor / self_mutation.rb
Created February 12, 2015 23:44
returning self means mutation
# If you return self you have either mutated self, mutated something else (essentially global, even worse)
# or thrown away the change as a NOOP. Let's play with examples?
namespace :db do
task :migrate => :clear_load_paths do
Dependencies.load_paths = @old_load_paths
end
task :clear_load_paths do
@old_load_paths = Dependencies.load_paths
Dependencies.load_paths = []
end
end
@knzconnor
knzconnor / method_defined_where.rb
Created December 16, 2008 19:16
Determining where in the inheritance chain a method is defined (ruby).
def Object.method_defined_where(method)
self.ancestors.detect { |a| a.methods(false).include?(method.to_s) }
end
#Usage - define in your .irbrc and then:
#File.method_defined_where('read')
#=> IO
#Alternatively it has been added to my copy of utility_belt: http://github.com/timocratic/utility-belt/commit/74397c4f8be135cf46f65ca509ddb90e2e47799e

Note to Self

Test

Tests build confidence. Write 'em. They'll save your ass, and they'll let you take a chainsaw to your code without being afraid of unintended consequences.

Automate

def foo(value)
"#{value}foo"
end
[1,2].map &method(:foo)
#=> ["1foo", "2foo"]
def foo(value)
"#{value[0]}foo"
end
[[1,2],[3,4]].map &method(:foo)
test = Rake::Task['test']
test.clear
desc 'Run all units, functionals, integration, libs'
task :test do
run_sets_of_tests(%w(test:units test:functionals test:integration test:libs))
end
@knzconnor
knzconnor / irb_from_script.rb
Created February 21, 2009 00:46
call irb from script context
#stolen from http://pastie.org/395671 twitter:@seacreature. Just repo'ed here so I can find it again
require "irb"
IRB.setup(nil)
IRB.conf[:MAIN_CONTEXT] = IRB::Context.new(IRB::Irb.new)
require "irb/ext/multi-irb"
IRB.irb(nil,binding)
class RspecInTU
class << self
alias it define_method
end
it "should say hello" do
puts "hello"
end
end
RspecInTU.new.send "should say hello"
#Or just check-out: http://github.com/timocratic/test_benchmark/tree/master
#It's only one file too: http://github.com/timocratic/test_benchmark/blob/832b2217451b49ed218d2db31dfad2b81a2399eb/lib/test_benchmark.rb
#Go ahead and steal anything you want from it - or even better fork it and add what features you like