Skip to content

Instantly share code, notes, and snippets.

@jjb
jjb / gist:3730588
Created September 16, 2012 00:43
raising an exception inside a child thread
def do_jobs
begin
while true
# do interesting things
end
rescue NoMoreDiskSpace
# tidy things up
end
end
@jjb
jjb / 1. code.rb
Created September 16, 2012 01:32
Timeout raising an error wherever the inner code happens to be
require 'timeout'
Timeout.timeout(rand) do # random number between 0 and 1 (seconds)
sleep 0.1
sleep 0.1
sleep 0.1
sleep 0.1
sleep 0.1
sleep 0.1
sleep 0.1
@jjb
jjb / gist:3733242
Created September 16, 2012 17:03
Restart httpd when it is eating memory
#!/bin/env ruby
require 'rubygems'
require 'pony'
# total memory used
total_memory_used = `free | awk '$1=="Mem:" {print $3 / $2 * 100}'`.to_i
# active memory used
used = `awk '$1=="Active:"{active=$2}$1=="MemTotal:"{total=$2}END{print active/total * 100}' /proc/meminfo`.to_i
2012-09-27 18:08:53.258 growlnotify[83684:707] Got disconnected: Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo=0x7fb3e2a0f900 {NSLocalizedDescription=Connection refused, NSLocalizedFailureReason=Error in connect() function}
2012-09-27 18:08:53.260 growlnotify[83684:707] <GrowlGNTPRegistrationAttempt: 0x7fb3e2a0f770> failed because Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo=0x7fb3e2a0f900 {NSLocalizedDescription=Connection refused, NSLocalizedFailureReason=Error in connect() function}
2012-09-27 18:08:53.260 growlnotify[83684:707] Failed to register with (null)
@jjb
jjb / 1-newline.txt
Created November 4, 2012 03:31
Files for newline demo
foo
bar
@jjb
jjb / diff.diff
Created November 4, 2012 03:40
commandline tools dealing with missing trailing newline
# diff -c 1.txt 3.txt - lack of newline is announced in output
*** 1.txt 2012-11-03 23:24:05.000000000 -0400
--- 3.txt 2012-11-03 23:28:44.000000000 -0400
***************
*** 1,2 ****
! foo
bar
\ No newline at end of file
--- 1,2 ----
! food
@jjb
jjb / 1. without-newline.diff
Created November 4, 2012 03:51
diff dealing with absent vs. present newline when new code is appended
# diff -c 1.txt 2.txt
*** 1.txt 2012-11-03 23:24:05.000000000 -0400
--- 2.txt 2012-11-03 23:24:13.000000000 -0400
***************
*** 1,2 ****
foo
! bar
\ No newline at end of file
--- 1,3 ----
foo
@jjb
jjb / gist:4043594
Created November 9, 2012 03:54
putting .bin in PATH only when there is a Gemfile present
function chpwd {
if [ -r $PWD/Gemfile ]; then
export PATH=./bin:${PATH//\.\/bin:}
else
regexp-replace PATH '\./bin:' ''
fi
}
def my_method(arg1, arg2)
unless Foo == arg1.class
raise ArgumentError,
"A Foo is expected for the first argument. An #{arg1.class} was given."
end
unless Bar == arg2.class
raise ArgumentError,
"A Bar is expected for the first argument. An #{arg2.class} was given."
end
@jjb
jjb / 1. code.rb
Last active December 14, 2015 00:29
class Object
def self.typecheck(object)
unless self == object.class
raise ArgumentError,
"expected type: #{self}. given: #{object.class}."
end
end
end
class Foo; end