Skip to content

Instantly share code, notes, and snippets.

View amatsuda's full-sized avatar

Akira Matsuda amatsuda

View GitHub Profile
@amatsuda
amatsuda / gist:1187873
Created September 2, 2011 03:41
% v foo_bar.rb:100 starts vim at line 100
# put this function in your .zshrc
function v() {vim ${${=*/:/ +}/:*}}
@amatsuda
amatsuda / http.rb
Created November 1, 2011 12:11
A sample of Ruby's method_missing
require 'open-uri'
class Http
def self./(url)
open("http://#{url}").read
end
end
class String
def com
self + '.com'
@amatsuda
amatsuda / gist:1360650
Created November 12, 2011 15:11
String#gsub: String vs Regexp
ruby-1.9.3-p0> Benchmark.ms { 100000.times { 'a-b-c-d-e'.gsub /-/, '_' } }
=> 422.281
ruby-1.9.3-p0> Benchmark.ms { 100000.times { 'a-b-c-d-e'.gsub '-', '_' } }
=> 471.096
@amatsuda
amatsuda / cp_to_bitcasa.rb
Created January 29, 2012 11:20
キャッシュでローカルディスクが溢れないようにちょっとずつBitcasaにコピるスクリプト
SRC_DIR = '/Users/a_matsuda/Music/iTunes/iTunes Media/Music'
TARGET_DIR = '/Users/a_matsuda/Bitcasa/backup/Music/iTunes/iTunes Media/Music'
def cache_is_empty?
s = `du -s ~/Library/Caches/com.bitcasa.Bitcasa/Data/bks/outgoing | awk '{print $1}'`
s.to_i.zero?
end
def already_copied?(artist)
File.exists? "#{TARGET_DIR}/#{artist}"
class Hash
def try(*a, &b)
if (a.size == 1) && a.first.is_a?(Array) && (a.first.size == 1)
self.fetch a.first.first
else
super
end
end
end
@amatsuda
amatsuda / cookpadish_template.rb
Created April 27, 2012 08:40
a sample Rails app
git :init
git :add => '.'
git :commit => "-m 'Initial commit'"
remove_file 'public/index.html'
git :add => 'public'
git :commit => "-a -m 'rm public/index.html'"
generate :model, 'user name'
generate :model, 'recipe title user:references'
#!/bin/zsh
for b in `git branch -r | grep "^ origin" | cut -c 10- | fgrep -v HEAD | grep -v master`; do
git checkout -t origin/${b}
done
@amatsuda
amatsuda / deploy.rb
Created November 15, 2012 06:55
perform precompile only when any of the asset files has changed since the last deploy
# do not shallow_clone from Git repo
namespace :deploy do
namespace :assets do
# perform precompile only when any of the asset files has changed since the last deploy
task :precompile, :roles => :web, :except => {:no_release => true} do
from = source.next_revision(current_revision)
asset_changing_files = ['vendor/assets/', 'app/assets/', 'lib/assets', 'Gemfile', 'Gemfile.lock'].select {|f| File.exists? f}
if capture("cd #{latest_release} && #{source.local.log(from)} #{asset_changing_files.join(' ')} | wc -l").to_i > 0
@amatsuda
amatsuda / gist:4158531
Created November 28, 2012 01:44
headers, cookies, and params usage in my controllers
% rake stats
+----------------------+-------+-------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers | 4327 | 3478 | 110 | 361 | 3 | 7 |
| Helpers | 384 | 331 | 0 | 35 | 0 | 7 |
| Models | 2912 | 2177 | 84 | 226 | 2 | 7 |
| Libraries | 281 | 230 | 8 | 39 | 4 | 3 |
| Model specs | 3096 | 2663 | 0 | 0 | 0 | 0 |
| Controller specs | 1005 | 783 | 0 | 3 | 0 | 259 |
@amatsuda
amatsuda / failing_migration_test.rb
Created January 21, 2013 11:30
a failing AR table_name_{prefix,suffix} test
require 'cases/helper'
require 'cases/migration/helper'
require MIGRATIONS_ROOT + '/valid/2_we_need_reminders'
class Reminder < ActiveRecord::Base; end
class FailingMigrationTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false