Skip to content

Instantly share code, notes, and snippets.

@tpope
tpope / find_first_or_nil.rb
Last active September 14, 2015 23:34 — forked from johana-star/find_first_or_nil.rb
A way to fing the first thing that matches or set to nil
grading_period = @context.courses.detect do |course|
gp = GradingPeriod.context_find(course, params[:grading_period_id]) and break gp
end
@tpope
tpope / to_set_performance.rb
Created September 5, 2011 01:06 — forked from h3h/to_set_performance.rb
Don't Convert Arrays to Sets for Inclusion Checks
ids = (1..1_000_000).to_a; nil
x = rand(1_000_000)
Benchmark.realtime do
the_set = ids.to_set
1000.times do
the_set.include?(x)
end
end
# => 0.7543990612030029
@tpope
tpope / _gem
Created August 27, 2011 18:58 — forked from alexvollmer/_gem
zsh completion for rubygems
#compdef gem gem1.9
gem_general_flags=("(-h --help)"{-h,--help}"[Get help on this command]"
"(-V --verbose)"{-V,--verbose}"[Set the verbose level of output]"
"(-q --quiet)"{-q,--quiet}"[Silence commands]"
"--config-file[Use this config file instead of default]:file:_files"
"--backtrace[Show stack backtrace on errors]"
"--debug[Turn on Ruby debugging]"
$nul_arg
)
@tpope
tpope / morse.rb
Created June 11, 2010 03:50 — forked from ryanb/morse.rb
def morse s;s.upcase.gsub(/\S/){|x|("CF+(514M )DGL"[x[0]-65]).to_s(3).tr("12",".-")};end
@tpope
tpope / blank_context.rb
Created December 12, 2008 07:27 — forked from nakajima/blank_context.rb
Now free of pesky instance_ methods!
module Kernel
def blank_context(*args, &block)
ivars = args.extract_options!
args.push(/^__/)
klass = Class.new do
instance_methods.each do |m|
undef_method(m) unless args.any? { |pattern| m =~ pattern }
end