Skip to content

Instantly share code, notes, and snippets.

View tiegz's full-sized avatar
👋
Working from home

Tieg Zaharia tiegz

👋
Working from home
View GitHub Profile
@tiegz
tiegz / tracer.rb
Created July 26, 2012 21:37
Trying to implement a pure-ruby version of Memprof
puts "Experiment... don't use this on a big chunk of code."
if RUBY_VERSION != "1.9.3"
throw "This has only been tested on Ruby 1.9.3"
end
require 'stringio'
require 'pp'
class Tracer
@tiegz
tiegz / string_utf8.rb
Created July 11, 2012 19:54
utf8 helper for 1.8/1.9 upgrade
# A simple string helper compatible with both 1.8 and 1.9. Avoids
# duplication of force_encoding('utf-8').
class String
def utf8
respond_to?(:force_encoding) ? force_encoding('utf-8') : self
end
end
@tiegz
tiegz / 1_8_string_force_encoding.rb
Created July 11, 2012 15:06
force_encoding monkeypatch for Ruby 1.8
if RUBY_VERSION =~ /1\.8/
class String
def force_encoding(encoding)
self
end
end
end
# 1.8.7 :025 > Benchmark.ms { 10000.times { x = "" } }
# => 2.4268627166748
@tiegz
tiegz / average_column_lengths.rb
Created July 6, 2012 19:06
ActiveRecord::Base.average_column_lengths
require 'active_support/concern'
module AverageColumnLength
extend ActiveSupport::Concern
# Returns a map of column names to their average row
# length (in bytes), based on a +sample_size+ of rows.
# Warning +random=true+ slows it down.
#
# The +avg_row_length+ returned from MySQL's +SHOW
# TABLE STATUS+ is often innacurate. You can calculate
@tiegz
tiegz / set_trace_func_experiment.rb
Created June 26, 2012 17:33
set_trace_func experiment
x = Hash.new(0)
orig = ObjectSpace.count_objects
set_trace_func proc { |event, file, line, id, binding, classname|
ObjectSpace.each_pair { |k,v|
x["#{file}:#{line}:#{k}"] += [0,(v - orig[k])].max
}
}
@tiegz
tiegz / array_one?_method_usage.rb
Created June 9, 2012 05:58
Using the method Array#one? in ruby
# The method Array#one? in ruby is intended to be used with a block. It does *not* signify that
# there is one element in the array:
1.8.7 :066 > [1,2,3].one?
=> false
1.8.7 :067 > [1,nil,nil].one?
=> true
# Also, it is slower than just checking the array's size:
@tiegz
tiegz / visualize_arel.rb
Created May 7, 2012 20:28
Visualizing ARel ASTs from your ActiveRecord queries
# Add the graphiz gem to your Gemfile
gem "ruby-graphviz"
# And in the rails console, try the following:
require 'graphviz'
arel = User.where(:name => "Sadako Yamamura").where(:location => "Tokyo").limit(1).arel
graph = GraphViz.parse_string(arel.to_dot)
graph.output(:png => "user_query.png")
@tiegz
tiegz / class_attribute_behavior.rb
Created March 28, 2012 15:29
The behavior of superclasses and classes with Rails 3.2 class_attribute
// Key difference: the old way of using {write,read}_inheritable_attribute was
// a *static default* for child classes. The new way of using class_attribute is a
// *dynamic default*.
class A
class_attribute :foo
self.foo = 1
end
class B < A; end
class C < B; end
@tiegz
tiegz / class_inheritable_attribute_pattern.rb
Created March 28, 2012 15:07
Pattern for class inheritable_attributes that was removed from rails 3.2
class SomeClass
self.instance_variable_set('@my_attribute', {:class => true})
def self.my_attribute; @my_attribute; end
def self.inherited(klass)
val = self.instance_variable_get('@my_attribute')
klass.instance_variable_set('@my_attribute', val.duplicable? ? val.dup : val)
super
end
end
@tiegz
tiegz / boxee.js
Created March 24, 2012 17:50
boxee.js
boxee.apiMinVersion=7.0;
boxee.reloadOnPageChange = true;
boxee.setMode(boxee.CURSOR_MODE);
boxee.onKeyboardKeyDown = function() {
document.write("TIEG")
}
boxee.onMouseMoveDown = function() {
document.write("MOUSE MOVE DOWN");
}