Skip to content

Instantly share code, notes, and snippets.

@mirakui
mirakui / jpeg_markers.rb
Created September 17, 2012 02:53
Extract width and height from JPEG format
module JpegMarkers
MARKERS = Hash[*%W(
SOF0 \xff\xc0
SOF1 \xff\xc1
SOF2 \xff\xc2
SOF3 \xff\xc3
DHT \xff\xc4
SOF5 \xff\xc5
SOF6 \xff\xc6
SOF7 \xff\xc7
@mirakui
mirakui / memo.txt
Created September 2, 2012 12:48
ffmpeg+ImageMagickによる分割アニメーションGIF作成コマンドメモ
# 元の動画をトリミング
ffmpeg -i DSC_3872.avi -vcodec copy -acodec copy -ss '00:00:04.33' -t '00:00:05.48' trim01.avi
# 横幅500px(tumblrの上限)にリサイズしつつ animated gif に変換
convert -resize 500 trim01.avi trim01.gif
# 明るさ・コントラストをいじりつつ63色に減色(クロップ処理後に1MBに収まるように色数調整)
convert -brightness-contrast 40x40 -colors 63 trim01.gif trim01-v3.gif
# 横3x縦2にクロップする(画像同士の間隔は10px)
convert -crop 160x135+0+0 +repage trim01-v3.gif trim01-v3-1.gif
convert -crop 160x135+170+0 +repage trim01-v3.gif trim01-v3-2.gif
convert -crop 160x135+340+0 +repage trim01-v3.gif trim01-v3-3.gif
@mirakui
mirakui / unicorn_restart.rb
Created August 27, 2012 03:54
Restarting Unicorn Synchronously
#!/usr/bin/env ruby
require "optparse"
def usage
STDERR.puts "usage: #{$0} /path/to/unicorn.pid [-t timeout_sec]"
end
timeout = 60
pidfile = ARGV.shift
@mirakui
mirakui / foo_controller.rb
Created August 20, 2012 15:18
大域的なperform_cachingの値にかかわらずcaches_actionのテストをする
class FooController < ApplicationController
caches_action :index
def index
render :text => Time.now
end
end
# coding: utf-8
class Hoge
attr_accessor :hoge?
end
hoge = Hoge.new
hoge.hoge? = true
p hoge.hoge? # => true
@mirakui
mirakui / benchmarker.txt
Created June 12, 2012 14:09
WTF profiler and benchmarker in Rails 3.2
% rails -v
Rails 3.2.5
% rails benchmarker -h
Usage: rails benchmarker 'Ruby.code' 'Ruby.more_code' ... [OPTS]
-r, --runs N Number of runs.
Default: 4
-o, --output PATH Directory to use when writing the results.
Default: tmp/performance
-m, --metrics a,b,c Metrics to use.
Default: wall_time,memory,objects,gc_runs,gc_time
@mirakui
mirakui / how-to-use
Created May 22, 2012 14:04
Remote operation script
$ cat script.txt
grep hoge /var/log/nginx/access.log
$ rop.rb -H 'app-0{01..05}' -f script.txt
/tmp/rop/0522-2306-11/app-001
/tmp/rop/0522-2306-11/app-002
/tmp/rop/0522-2306-11/app-003
/tmp/rop/0522-2306-11/app-004
/tmp/rop/0522-2306-11/app-005
@mirakui
mirakui / readonly_console.rb
Created May 1, 2012 02:08
readonly rails console using Arproxy
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands/console'
require APP_PATH
Rails.application.require_environment!
require "arproxy"
module Arproxy
class Readonly < Base
@mirakui
mirakui / gist:1443041
Created December 7, 2011 14:38
puts with format
[STDOUT, STDERR].each do |io|
io.instance_eval do
def puts_with_format(*arg)
print "[#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} ##{$$}] "
puts_without_format *arg
end
alias puts_without_format puts
alias puts puts_with_format
end
end
@mirakui
mirakui / nenshu.rb
Created December 1, 2011 13:36
うわっ…私の二分探索ショボすぎ…?
require "rubygems"
require "rest-client"
OFFSET = 700
STEP = 100
def exist?(url)
res = RestClient.head url
res.code == 200
rescue