Skip to content

Instantly share code, notes, and snippets.

@benweint
benweint / cheap.rb
Last active December 19, 2015 04:38
CheapStrings - how do they work? (Original idea courtesy Charlie Somerville - https://gist.github.com/charliesome/5876753)
#!/usr/bin/env ruby
N=10_000_000
module CheapString
def `(s); s; end
end
class Stringer
include CheapString
#!/usr/bin/env ruby
def outer
outer2
rescue Exception => e
puts "Caught error: #{e}"
puts e.backtrace.join("\n")
end
def outer2
@benweint
benweint / set.rb
Last active December 21, 2015 11:09
Stupid Ruby Set benchmark
#!/usr/bin/env ruby
require 'set'
require 'benchmark'
nerrors = 1
iterations = 1000000
def count_allocations(label, iterations)
stat_before = GC.stat
@benweint
benweint / frobnicate.rb
Created August 29, 2013 05:19
ActiveSupport::Notifications thread safety demo
#!/usr/bin/env ruby
require 'active_support'
class Frobnicator
def frobnicate
ActiveSupport::Notifications.instrument('frobnicate') do
puts 'frobnicating'
end
end
@benweint
benweint / bitfield.rb
Created September 11, 2013 15:13
bitfield.rb
#!/usr/bin/env ruby
# This implementation is adapted from Peter Cooper's - original available here:
# https://github.com/peterc/whatlanguage/blob/master/lib/whatlanguage/bitfield.rb
class ArrayBitField
attr_reader :size
attr_accessor :field
include Enumerable
ELEMENT_WIDTH = 32
@benweint
benweint / threads-across-fork.rb
Last active May 12, 2016 08:28
threads across fork Ruby demo
#!/usr/bin/env ruby
# Pass 'fork' as the first argument to use Process.fork, otherwise Process.daemon is used.
do_fork = (ARGV[0] == 'fork')
thread = Thread.new do
loop { sleep(1) }
end
@benweint
benweint / nrdebug-extra
Created September 30, 2013 20:44
nrdebug-extra
#!/usr/bin/env ruby
# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
require 'tempfile'
require 'rbconfig'
def fail(msg, opts={})
@benweint
benweint / as-note-thread-safety-demo.rb
Last active April 22, 2017 15:28
This is a demo of a thread-safety issue with ActiveSupport::Notifications for Rails issue https://github.com/rails/rails/issues/12069
#!/usr/bin/env ruby
#
# When run against rails/rails commit 91684fb193a91671d682701cc1357e7b4b3fbe2b,
# this code produces the following output on my machine:
#
# long start @ 0.000
# short start @ 0.510
# short finish @ 1.511
# short reported 1.511186 s
@benweint
benweint / nested.rb
Last active December 27, 2015 09:49
Demonstration of a segfault when parsing deeply-nested JSON arrays with yajl-ruby
#!/usr/bin/env ruby
require 'yajl'
depth = ARGV[0].to_i
root = []
a = root
depth.times { a << []; a = a[0] }
puts Yajl::Encoder.encode(root)
@benweint
benweint / setup.sh
Created November 6, 2013 06:53
My Ruby setup
# Install ruby-build
git clone [email protected]:sstephenson/ruby-build.git ~/.ruby-build
cd ~/.ruby-build
./install.sh
# List definitions
ruby-build --definitions
# Install some Rubies
ruby-build jruby-1.7.6 ~/.rubies/jruby-1.7.6