Skip to content

Instantly share code, notes, and snippets.

View jeremy's full-sized avatar

Jeremy Daer jeremy

View GitHub Profile
# Users: JSON.encode(whatever)
#
# Implementors: override dump_json(options) returning a Hash, Array, etc
# or: override encode_json(encoder) using the encoder API to return a JSON string
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/object/instance_variables'
module ActiveSupport
module JSON
# $/ is the ruby line separator, defaults to \n
$/ = '~'
File.open('some_file') do |file|
file.each_line do |line|
# ...
end
end
module AfterTransaction
def self.included(base)
base.extend(ClassMethods)
base.class_eval do
class <<self
alias_method_chain :transaction, :callbacks
end
end
end
# encoding: utf-8
☂ = Class.new { def ☎; puts "⎆" end }
☂.new.☎
jeremy@foo rails (master) $ /usr/local/ruby/1.8.6-ree/bin/ruby -v
ruby 1.8.6 (2008-08-08 patchlevel 286) [i686-darwin9.5.0]
jeremy@foo rails (master) $ /usr/local/ruby/1.8.6-ree/bin/ruby profile_requires.rb active_support
3757.52 KB 110258 obj 87.3 ms active_support
104.50 KB 2758 obj 3.3 ms active_support/json
16.50 KB 586 obj 0.4 ms active_support/json/decoding
78.95 KB 1901 obj 2.7 ms active_support/json/encoding
3.26 KB 54 obj 0.1 ms active_support/json/encoders/true_class
5.50 KB 97 obj 0.2 ms active_support/json/encoders/time
3.21 KB 58 obj 0.2 ms active_support/json/encoders/symbol
jeremy@foo rails (master) $ ruby -v
ruby 1.8.8dev (2009-03-19 revision 23009) [i386-darwin9.6.2]
jeremy@foo rails (master) $ ./profile_requires.rb active_support
5854.24 KB 222568 obj 139.9 ms active_support
171.17 KB 2629 obj 4.1 ms active_support/json
23.53 KB 601 obj 0.5 ms active_support/json/decoding
136.72 KB 1766 obj 3.3 ms active_support/json/encoding
6.49 KB 45 obj 0.2 ms active_support/json/encoders/true_class
8.50 KB 88 obj 0.2 ms active_support/json/encoders/time
6.47 KB 49 obj 0.2 ms active_support/json/encoders/symbol
#!/usr/bin/env ruby
# Run from toplevel rails checkout to profile a component's requires:
# $ ./profile_requires.rb active_support
GC.enable_stats
require 'rubygems'
require 'benchmark'
ENV['NO_RELOAD'] ||= '1'
ENV['RAILS_ENV'] ||= 'development'
$ irb
>> String.instance_method(:to_s).name
=> "to_s"
$ irb-1.9
>> String.instance_method(:to_s).name
=> :to_s
$ jirb
>> String.instance_method(:to_s).name
#!/usr/bin/env ruby
class FooArray < Array; end
a = FooArray.new(1000000){'a'}
a = nil
GC.start
count = ObjectSpace.each_object(FooArray) {}
puts "#{count} objects remain after garbage collection"
$ ruby-1.9 -e "module A; B = 1 end; class C; include A end; puts C.constants(false).inspect"
[]
$ jruby --1.9 -e "module A; B = 1 end; class C; include A end; puts C.constants(false).inspect"
:1:in `constants': wrong # of arguments(1 for 0) (ArgumentError)
from :1