Skip to content

Instantly share code, notes, and snippets.

@benweint
benweint / build-rubies.rb
Created December 23, 2013 17:49
build-rubies
#!/usr/bin/env ruby
#
# This is a proof-of-concept script to build every commit of ruby over the past
# year and store all of the resulting builds in git. The purpose of using git
# for storage is to take advantage of its de-duplication and compression
# features.
#
# Requirements:
# - git
@benweint
benweint / Gemfile
Last active December 31, 2015 20:39
Rubinius Rails segfault example
gem 'rails', '~>4.0.2'
gem 'rubysl', :platforms => [:rbx]
gem 'racc', :platforms => [:rbx]
@benweint
benweint / encoding.rb
Created November 8, 2013 06:05
String encoding demo
#!/usr/bin/env ruby
require 'benchmark'
length = 512
iterations = 100000
encodings = Encoding.name_list
Benchmark.bmbm do |r|
r.report("encode") do
@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
@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 / 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 / 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 / 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 / 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 / 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