Skip to content

Instantly share code, notes, and snippets.

@joel
joel / gist:3945822
Created October 24, 2012 12:32
tap or not tap
#!/usr/bin/env ruby
require 'benchmark'
N = 1000000
class TestingObjectTap
def ugly
results = {}
[:x, :y, :z].each do |letter|
results[letter] = rand(100)
@joel
joel / gist:3938318
Created October 23, 2012 11:41
inject vs reduce
require 'benchmark'
class Foo
def initialize
@bar = rand(100) # for caching
end
def bar
@bar ||= rand(100)
end
end
@joel
joel / gist:3918080
Created October 19, 2012 12:47
config.ru UTF-8
# Not override -E option or LANG and apply only for ruby > 1.9
if Object.const_defined?(:Encoding) && !ENV['LANG'] && !ENV['RUBYOPT'].include?('-E')
Encoding.default_internal = 'utf-8'
Encoding.default_external = 'utf-8'
end
require ::File.expand_path('../config/environment', __FILE__)
@joel
joel / gist:3904601
Created October 17, 2012 09:04
Benchmark of String concatenation
require 'benchmark'
N = 70000
Benchmark.bmbm do |x|
x.report('foo + bar') { N.times {
'foo' + 'bar'
} }
x.report('#{foo}#{bar})') { N.times {
"#{'foo'}#{'bar'}"
@joel
joel / gist:3898372
Created October 16, 2012 09:43
Make a Ruby Gem
http://guides.rubygems.org/make-your-own-gem/
rvm use ruby-1.9.3-p0@lorem --create
echo "rvm use ruby-1.9.3-p0@lorem --create" >> lorem/.rvmrc
bundle gem lorem
gem build lorem.gemspec
git tag -a v0.0.1 -m 'version 0.0.1'
git push --tags
gem push lorem-0.0.1.gem
My gist
@joel
joel / gist:3090583
Created July 11, 2012 14:08
ARRAY.reject! { |c| c.empty? } vs ARRAY.reject!(&:empty?)
require 'benchmark'
N = 10000
ARRAY = begin
[].tap do |array|
(1..1000).to_a.each do |entry|
array << '' if rand(5) == 0
array << entry.to_s
end
@joel
joel / gist:3076423
Created July 9, 2012 12:59
Comparaison blank? present? Nil ?
require 'rubygems'
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'rails/all'
N = 10000000
EMPTY_STRING = ''
@joel
joel / .irbrc
Created July 9, 2012 09:21
Make all factory girl is available in your rails console
# mate ~/.irbrc
#!/usr/bin/env ruby
if defined? Rails
begin
require 'factory_girl'
Dir[Rails.root.join("spec/factories/**/*.rb")].each { |f| require f }
rescue LoadError
end
@joel
joel / fix_homebrew.rb
Created July 6, 2012 10:10
Fix HomeBrew Binary unlinked after update Gcc
#!/usr/bin/env ruby
class CleaningHomeBrew
# Your binaries unlinked
BINARIES = ['cmake', 'imagemagick', 'jasper', 'jpeg', 'libevent', 'libtiff', 'little-cms','memcached','mysql','ossp-uuid','pidof','postgresql','redis','sphinx','wget']
class << self
def clean