Skip to content

Instantly share code, notes, and snippets.

View amatsuda's full-sized avatar

Akira Matsuda amatsuda

View GitHub Profile
% rails g model m `ruby -e "801.times {|i| print %(c#{i} )}"` && rake db:migrate
% rails r 'p M.new.methods.length'
#=> 10000
@amatsuda
amatsuda / shadowing_outer_local_variable.rb
Last active August 29, 2015 14:25
I do understand what's happening, but...
if false
a = 2
p a
else
Proc.new do |a|
p a
end
end
+----------------------+--------+--------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+--------+--------+---------+---------+-----+-------+
| Controllers | 48726 | 39215 | 522 | 3970 | 7 | 7 |
| Helpers | 14795 | 12125 | 15 | 1399 | 93 | 6 |
| Models | 96678 | 76140 | 1747 | 8548 | 4 | 6 |
| Mailers | 2208 | 1766 | 44 | 206 | 4 | 6 |
| Workers | 639 | 540 | 20 | 31 | 1 | 15 |
| Chanko units | 11713 | 9644 | 6 | 247 | 41 | 37 |
| Libraries | 50409 | 41650 | 592 | 3741 | 6 | 9 |
@amatsuda
amatsuda / database_cleaner_bench.rb
Last active December 23, 2015 06:29
Want to know how much time you're spending for cleaning database before and after each test?
# put this piece of code in your spec/spec_helper.rb or spec/support/any_rb_file.rb
# then run `rake spec`
module CleanerBench
def clean(*)
now = Time.now
super
@time_spent ||= 0
@time_spent += Time.now - now
end
Kernel.class_eval do
#FIXME this is terrible
def static(meth)
define_singleton_method(meth) do |*args, &b|
new.send meth, *args, &b
end
meth
end
def abstract(meth)
@amatsuda
amatsuda / lvar.rb
Last active December 20, 2015 16:29
if false
a = 1
end
p a
#=> nil
p b
#=> undefined local variable or method `b' for main:Object (NameError)
S = 'foo'
class A
def a() S; end
end
class B < BasicObject
# needs to be ::S
def b() ::S; end
end
@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
@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 / 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