Skip to content

Instantly share code, notes, and snippets.

@mdub
mdub / getoptlong-test.rb
Created Apr 24, 2012
Play with getoptlong
View getoptlong-test.rb
require 'getoptlong'
opts = GetoptLong.new(
[ "--name", "-n", GetoptLong::REQUIRED_ARGUMENT ],
[ "--help", "-h", GetoptLong::NO_ARGUMENT ],
[ "--version", "-v", GetoptLong::NO_ARGUMENT ]
)
opts.each do |opt, arg|
p [opt, arg]
@mdub
mdub / output.txt
Created Mar 21, 2012
Ruby lazy enumerable benchmark
View output.txt
$ ruby --version
ruby 2.0.0dev (2012-03-20 trunk 35094) [x86_64-darwin11.3.0]
$ ruby pipeline_bench.rb
IMPLEMENTATION take(10) take(100) take(1000) to_a
conventional (eager) 0.01416 0.01407 0.01422 0.01411
enumerating 0.00003 0.00007 0.00063 0.03140
lazing 0.00004 0.00011 0.00098 0.04759
ruby2 Enumerable#lazy 0.00004 0.00013 0.00112 0.05304
facets Enumerable#defer 0.00005 0.00018 0.00162 0.07108
@mdub
mdub / gist:2129095
Created Mar 20, 2012
toplevel constant Bar referenced by Foo::Bar
View gist:2129095
>> class Foo; end
=> nil
>> module Bar; end
=> nil
>> Foo::Bar
(irb):3: warning: toplevel constant Bar referenced by Foo::Bar
=> Bar
>> Bar::Foo
NameError: uninitialized constant Bar::Foo
@mdub
mdub / 0-readme.md
Created Feb 28, 2012 — forked from albertoperdomo/0-readme.md
ruby-1.9.3-p125 cumulative performance patch.
View 0-readme.md

Patched ruby 1.9.3-p125 for 30% faster rails boot

What is?

This script installs a patched version of ruby 1.9.3-p125 with patches to make ruby-debug work again (#47) and boot-time performance improvements (#66 and #68), and runtime performance improvements (#83 and #84).

Huge thanks to funny-falcon for the performance patches.

View snippet.rb
# this is fine
params = {
"mode" => "update"
"people" => {
"jackie"=> "Jackie Watkins"
"max"=> "Max Edwards"
}
}
# this is not
@mdub
mdub / postgresql_full_text_search.rb
Created Aug 3, 2011
PostgreSQL full-text search support for ActiveRecord
View postgresql_full_text_search.rb
module PostgresqlFullTextSearch
extend ActiveSupport::Concern
module ClassMethods
def tsearch(column_names, terms)
raise(ArgumentError) if terms.nil?
return self.scoped if terms == ""
vectors = Array(column_names).map do |column_name|
@mdub
mdub / newrelic-fiber-hack.rb
Created Jul 26, 2011
Patch the NewRelic RPM agent to allow for use of Fibers
View newrelic-fiber-hack.rb
require 'new_relic/control'
# monkey-patch NewRelic::Agent::TransactionSampler to avoid storing the
# current TransactionSampleBuilder in a Thread-local, as it does not play nicely
# in the presence of Fibers
module NewRelic
module Agent
class TransactionSampler
@mdub
mdub / gitdown.rb
Created Jun 14, 2011
Clamp global option example
View gitdown.rb
rrequire "clamp"
class Logger
DEFAULT_LEVEL = 0
attr_accessor :log_level
def initialize
@log_level = DEFAULT_LEVEL
@mdub
mdub / use_render_main.html.haml
Created May 15, 2011
Example of using "render_main" markup builder
View use_render_main.html.haml
= render_main do |main|
- main.action_ribbon do
%p ACTION CONTENT
%p MAIN CONTENT
- main.sidebar do |sidebar|
- sidebar.header do
@mdub
mdub / pgbackup.rake
Created May 12, 2011
Rake tasks to capture backups and archive in S3
View pgbackup.rake
# Tasks designed to run ON Heroku
namespace :pgbackup do
def pgbackup_client
require "pgbackups/client"
@pgbackup_client ||= PGBackups::Client.new(ENV.fetch("PGBACKUPS_URL"))
end
task :capture do
db = "DATABASE_URL"