This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} |