Skip to content

Instantly share code, notes, and snippets.

@macks
macks / restart-unicorn.rb
Created December 29, 2011 12:57
Graceful restart script for Unicorn
#!/usr/bin/ruby
# restart-unicorn: Graceful restart for Unicorn
# depends on Linux's proc(5)
#
# MIT License: Copyright(c)2011 MATSUYAMA Kengo
require 'scanf'
require 'timeout'
class ProcFS
@macks
macks / commalize.rb
Created December 27, 2011 10:48
Integr#commalize
class Integer
def commalize
to_s.gsub(/\d(?=.{3}+$)/, '\0,')
end
end
if defined?(RSpec::Core::Runner)
describe 'Integer#commalize' do
[
[ 321, '321'],
@macks
macks / gist:1338880
Created November 4, 2011 08:00
https client for Rev (libev)
require 'rev'
require 'rev/ssl'
class MyHttpClient < Rev::HttpClient
attr_reader :response_header, :response_body
def on_connect
@response_body = []
super
end
@macks
macks / gist:961171
Created May 8, 2011 06:37
local variables in eval
require 'rubygems'
require 'rspec'
describe 'Local variable in eval' do
context 'Assignment in eval' do
it 'works' do
expect {
eval <<-EOS
var = true
raise unless var
@macks
macks / gist:937135
Created April 22, 2011 17:19
rprotoc session
~$ cat info_t.proto
message Info_t {
required string Category = 1;
required string Value = 2;
}
~$ rprotoc info_t.proto
./info_t.pb.rb writing...
~$ cat test.rb
@macks
macks / gist:885402
Created March 24, 2011 16:51
paste url and open in new tab (for Firefox's keyconfig add-on)
var paste = readFromClipboard();
if (paste)
gBrowser.selectedTab = gBrowser.addTab(paste);
@macks
macks / gist:846152
Created February 27, 2011 12:43
Show daily diffstat summary in svn working copy
#!/bin/sh
from=$(date -d "${1:-today}" +%Y-%m-%d)
to=$(date -d "${2:-tomorrow}" +%Y-%m-%d)
d=$from
while true; do
next=$(date -d "$d + 1day" +%Y-%m-%d)
echo -n "$d:"
@macks
macks / gist:804599
Created January 31, 2011 19:09
Sequel + Ruby/MySQL
Sequel.connect(
"mysql://user:password@host/database",
:compress => false # Required!
)
@macks
macks / icalendar_sample.rb
Created November 25, 2010 19:34
icalendar sample
require 'date'
require 'rubygems'
require 'bundler'
Bundler.setup
require 'icalendar'
cal = Icalendar::Calendar.new
cal.timezone do
tzid 'Tokyo/Asia'
standard do
@macks
macks / gist:595383
Created September 24, 2010 13:39
rprotoc session
macks@debian:~/test$ ls
test.rb test1.proto test2.proto
macks@debian:~/test$ cat test1.proto
package A;
message m1
{
required uint32 aaa = 1 [ default = 0 ];
optional uint32 bbb = 2 [ default = 0 ];
}
macks@debian:~/test$ cat test2.proto