Skip to content

Instantly share code, notes, and snippets.

# Demonstration showing that
#
# alias_method :bar, :foo
#
# is not equivalent to
#
# def bar
# foo
# end
# Collect _spec.rb and .feature files under spec/ that are relevant to current work, one per line. Good for pipes.
#
# 1. Modified, staged git files.
# 2. Modified, unstaged git files.
# 3. Changes in any commits since master.
# 4. New, untracked files.
#
# $ specs | xargs rspec
cat <(git diff --name-only) <(git diff --staged --name-only) <(git diff master..@ --name-only) <(git ls-files --others --exclude-standard) | uniq | grep -e "^spec/" | grep -E "_spec\.rb$|\.feature$"
@aprescott
aprescott / delay_method.rb
Created March 26, 2014 15:04
Slow down an instance method by 1 second.
# Delay Foo#method by 1 second before
# executing its original implementation.
def delay_method(klass, method)
sleeping_giant = Module.new do
define_method(method) do |*args, &block|
sleep 1
super(*args, &block)
end
end
class Symbol
def |(other)
self.to_proc | other
end
end
class Proc
def |(other)
proc { |arg| other.to_proc.call(self.call(arg)) }
end
@aprescott
aprescott / gist:1705d11ac5091a447311
Created October 31, 2014 18:48
automatically switch Terminal.app to a specific profile before ssh'ing
function prodssh { osascript -e 'tell application "Terminal" to set current settings of selected tab of window 1 to settings set "Red Sands"' && ssh "$@"; }
# prodssh yourserver
@aprescott
aprescott / proc_equality_1.8.7.rb
Created April 1, 2011 23:09
Proc#== behaviour.
#
# 1.8.7 docs: http://ruby-doc.org/core-1.8.7/classes/Proc.html#M000463
#
# Return true if prc is the same object as other_proc, or if they are both procs with the same body.
#
RUBY_DESCRIPTION #=> ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-linux]
proc { 1 } == proc { 1 } # => false
proc { 1 + 1 } == proc { 1 + 1 } # => false
@aprescott
aprescott / local_variable_references.rb
Created April 5, 2011 12:49
Finding local variable references within the current scope
>> local_variables
=> ["_"]
>> a = ""
=> ""
>> local_variables
=> ["_", "a"]
>> b = binding; local_variables.select { |var| b.eval(var).object_id == a.object_id }
=> ["a"]
>> x = a
=> ""
@aprescott
aprescott / pdf_bombs.html
Created April 14, 2011 22:08
Uses HTML5 data-* attributes and old-school CSS content to get around that problem of PDF links causing browser hangs because you didn't check the URL.
<!-- See it in action: http://jsfiddle.net/eVAg4/1/ -->
<!doctype html>
<head>
<meta charset="UTF-8">
<title>PDF link file sizes in CSS</title>
<style type="text/css">
a[href$=".pdf"]::after { content: " (PDF)"; }
a[href$=".pdf"][data-size]::after { content: " (PDF, " attr(data-size) ")"; }
</style>
@aprescott
aprescott / gist:960903
Created May 7, 2011 22:19
Find the lowest multiple of some number greater than or equal to some other number.
def foo(n, m)
return n if n % m == 0
n + m - (n % m)
end
foo(25, 7) #=> 28
foo(28, 7) #=> 28
foo(29, 7) #=> 35
# No branching.
@aprescott
aprescott / fullwidther.rb
Created May 29, 2011 01:07
Convert strings to their fullwidth versions
# it turns out that "A".ord - "A".ord is constant when you replace the two characters
# with, say "B" and "B", so ("B".ord + DIFFERENCE).to_s(16) == "ff22" and "\uff22"
# is "B". (Thanks, Unicode consortium.)
#
# an exception is U+0020 space, which has IDEOGRAPHIC SPACE U+3000, but the two don't
# differ by DIFFERENCE == 65248 == 0xfee0, so you have to replace 20+65248 == 0xff00
# with 0x3000.
DIFFERENCE = 65248