Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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
(gdb) t a a bt
Thread 5 (process 11796):
#0 0x00000001003d8e4f in llvm::SelectionDAG::getFrameIndex ()
#1 0x000000010027d55b in (anonymous namespace)::X86DAGToDAGISel::getAddressOperands ()
#2 0x000000010027da18 in (anonymous namespace)::X86DAGToDAGISel::SelectAddr ()
#3 0x000000010027a78c in (anonymous namespace)::X86DAGToDAGISel::CheckComplexPattern ()
#4 0x000000010043994c in llvm::SelectionDAGISel::SelectCodeCommon ()
#5 0x0000000100274536 in (anonymous namespace)::X86DAGToDAGISel::Select ()
#6 0x000000010043bc41 in llvm::SelectionDAGISel::DoInstructionSelection ()
@benweint
benweint / array-racc.rb
Created January 21, 2014 00:10
Array / racc / rbx bug demo
#!/usr/bin/env ruby
string = "\n\v"
puts "before racc require: #{Array(string).inspect}"
require 'racc'
puts "after racc require: #{Array(string).inspect}"
@benweint
benweint / string-subclass-encodings.rb
Created January 21, 2014 22:53
String subclasses and encodings in Rubinius
#!/usr/bin/env ruby
class BadString < String; end
original = (0..255).to_a.pack("C*").force_encoding("UTF-8")
subclass = BadString.new(original)
round_tripped = subclass.to_s
puts "encoding: original = #{original.encoding}, subclass = #{subclass.encoding}, round_tripped = #{round_tripped.encoding}"
puts "valid_encoding? original = #{original.valid_encoding?}, subclass = #{subclass.valid_encoding?}, round_tripped = #{round_tripped.valid_encoding?}"
@benweint
benweint / test.rb
Last active August 29, 2015 13:56
Demonstration of behavior difference between Ruby 1.9.3 and 2.0+
module A
def foo
puts "foo from A"
end
end
class B
include A
def foo
@benweint
benweint / standalone.rb
Created March 13, 2014 00:10
Ruby GC timing discrepancy
#!/usr/bin/env ruby
puts "Run this in another terminal:"
puts ""
puts " sudo ./trace-gc-standalone.sh #{$$}"
puts ""
puts "... wait for the 'Ready!' message, switch back here and press enter to start."
GC::Profiler.enable
$stdin.gets
@benweint
benweint / gc-profiler.rb
Created April 11, 2014 18:07
Demo of GC::Profiler.total time returning wrong units on JRuby
#!/usr/bin/env ruby
t0 = Time.now
GC::Profiler.enable
100000.times do
String.new('a' * 1000)
end
gc_time = GC::Profiler.total_time
@benweint
benweint / monitor-test.rb
Created April 30, 2014 20:01
Demonstration of issue with MonitorMixin on Ruby 1.8
#!/usr/bin/env ruby
require 'thread'
require 'rubygems'
require 'monitor'
class Dummy
include MonitorMixin
def sync