Skip to content

Instantly share code, notes, and snippets.

View kennethkalmer's full-sized avatar

Kenneth Kalmer kennethkalmer

View GitHub Profile
require 'prawn'
require 'benchmark'
pdf = Prawn::Document.new
cell = Prawn::Table::Cell.make( pdf, "Some text" )
n = 100_000
Benchmark.bm do |x|
x.report("cached") { n.times { cell.send(:styled_width_of_single_character) } }
@kennethkalmer
kennethkalmer / gist:3976646
Created October 29, 2012 21:25
jrlint backtrace
JRuby-Lint version 0.3.1
For more on gem compatibility see http://wiki.jruby.org/C-Extension-Alternatives
./Gemfile:8: [gems, warning] Found gem 'mysql2' which is reported to have some issues:
Use activerecord-jdbc-adapter instead along with jdbc-mysql.
./Gemfile:52: [gems, warning] Found gem 'thin' which is reported to have some issues:
Thin might compile and run but is not recommended. Try any one of the following JRuby-based servers: Trinidad, Mizuno, Kirk, TorqueBox or Puma.
./Gemfile:53: [gems, warning] Found gem 'unicorn' which is reported to have some issues:
Try any one of the following JRuby-based servers: Trinidad, Mizuno, Kirk, mobile or Puma.
./Gemfile:69: [gems, warning] Found gem 'nokogiri' which is reported to have some issues:
For best results, use the pure-Java version of Nokogiri (default after v1.5).
@kennethkalmer
kennethkalmer / deploy.rb
Created October 4, 2012 10:16
Capistrano config for near-zero downtime deployments
#
# Based heavily on the deployment recipe dicussed in the article at
# http://ariejan.net/2011/09/14/lighting-fast-zero-downtime-deployments-with-git-capistrano-nginx-and-unicorn
# but tweaked to fit our setup...
#
# NO WARRANTY, IMPLIED OR OTHERWISE
#
# Multistage setup
set :stages, %w(production staging)
@kennethkalmer
kennethkalmer / poltergeist.rb
Last active February 26, 2016 12:21
Save a PNG/HTML copy of the current page in Poltergeist when a scenario failed
# Save some data when a feature failed
After('@javascript') do |scenario|
# Only failed, and only if we can render
if scenario.failed? && page.driver.respond_to?(:render)
# Get a name
name = case scenario
when Cucumber::Ast::OutlineTable::ExampleRow
[ scenario.scenario_outline.name, scenario.name ].join(' ')
#
# An improvement on http://stackoverflow.com/a/9094206/284612
#
# Place this file in spec/support/signed_cookies.rb
#
module SignedCookies
def signed_cookie(name, opts={})
verifier = ActiveSupport::MessageVerifier.new(request.env["action_dispatch.secret_token".freeze])
if opts[:value]
@request.cookies[name] = verifier.generate(opts[:value])
@kennethkalmer
kennethkalmer / find_unused_helpers.rb
Created November 29, 2011 07:17
Find unused helpers in a Rails app (slow)
#!/usr/bin/env ruby
#
# Shotgun approach (read: slow and dirty hack) to help find unused helpers in a Rails application
#
puts "Loading all source files into memory :("
source = {}
Dir["app/**/**/*.*"].each do |f|
source[ f ] = File.readlines( f )
Rubinius Crash Report #rbxcrashreport
Error: signal SIGBUS
[[Backtrace]]
0 rbx 0x00000001000221f1 _ZN8rubiniusL12segv_handlerEi + 241
1 libSystem.B.dylib 0x00007fff8237f1ba _sigtramp + 26
[[System Info]]
sysname: Darwin
Rubinius Crash Report #rbxcrashreport
Error: signal SIGSEGV
[[Backtrace]]
0 rbx 0x00000001000221f1 _ZN8rubiniusL12segv_handlerEi + 241
1 libSystem.B.dylib 0x00007fff809681ba _sigtramp + 26
2 ??? 0x00007fff5fbf6b70 0x0 + 140734799768432
3 rbx 0x00000001002689cb rbx_yield_stack + 267
4 ??? 0x0000000102d7fec6 0x0 + 4342677190
@kennethkalmer
kennethkalmer / process.rb
Created August 25, 2011 18:07
RuoteAMQP error handling example
Ruote.process_definition :name => 'Test' do
sequence do
# Loop, depending on the amqp participant to set 'completed' once it has successfully processed the
# workitem.
_loop :break_if => "${f:completed}" do
# This remote participant will either set a field called 'completed' to true when it is done,
# or set 'error' with the exception.
amqp_participant :activity => "Do something"
@kennethkalmer
kennethkalmer / fix_assign.rb
Created July 21, 2011 16:54
Helper files for migrating from rspec-rails 1.3 to rspec-rails 2.6
#!/usr/bin/env ruby
#
# Migrate from old assigns[]= to assign()
Dir["spec/**/*_spec.rb"].each do |file|
changed = File.readlines( file ).map do |line|
pattern = /assigns\[(:[\w\d_]+)\] = (.+)$/
if line =~ pattern
puts "Match: #{line}"
puts "Assign: #{$1}"