Skip to content

Instantly share code, notes, and snippets.

View amatsuda's full-sized avatar

Akira Matsuda amatsuda

View GitHub Profile
#!/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 / 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'
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 / 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}"
@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 / 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: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 / Xresult
Created August 17, 2011 09:59
An RSpec matcher for asserting AR query
% rspec spec/models/user_spec.rb
.F.F.
Failures:
1) User
Failure/Error: it { should query :where => {:name => 'bar'} }
expected [:where] to query {:where=>{:name=>"bar"}}
# ./spec/models/user_spec.rb:6
require 'tempfile'
t1 = Tempfile.new 'foo.txt'
t2 = t1.dup
t1 = nil
GC.start
p File.exists?(t2.path)
p t2.read
@amatsuda
amatsuda / gist:960252
Created May 7, 2011 06:11
rails g scaffold + --skip on Rails 3.1
% rails -v
Rails 3.1.0.beta1
% ls app/models
user.rb
% rails g scaffold user name:string -s
invoke active_record
create db/migrate/20110507060923_create_users.rb
skip app/models/user.rb