Skip to content

Instantly share code, notes, and snippets.

View gist:ce54341db26ab1399a93
def recur(n)
if n == 0
bench
else
recur(n-1)
end
end
def bench
View habtm_bug.rb
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
View gist:3827440
An exception occurred running at_exit handlers
no block given (LocalJumpError)
Backtrace:
ERROR: the VM is exiting improperly running at_exit handlers
intended operation: :exception
associated value: nil
destination scope: unknown
An exception occurred
View gist:3668788
require 'thread'
bignum = (2 ** (0.size * 8))
start = 0
class H < Hash
def [](key)
super if key.kind_of?(Symbol)
end
@thedarkone
thedarkone / gist:3668383
Created Sep 7, 2012
This is not thread safe even on MRI
View gist:3668383
require 'thread'
bignum = (2 ** (0.size * 8))
start = 0
h = {:a => start}
h[:a] += bignum # get into the Bignum territory
iterations = 100_000
@thedarkone
thedarkone / gist:3645105
Created Sep 5, 2012
Ruby volatile APIs
View gist:3645105
# using accessors:
attr_volatile :table, :size_control
def check_for_resize
while (current_table = table) && MAX_CAP > (table_size = current_table.size) && RESIZING != (size_ctrl = size_control) && size_ctrl < @counter.sum
try_in_resize_lock(current_table, size_ctrl) do
self.table = rebuild(current_table)
(table_size << 1) - (table_size >> 1) # 75% load factor
end
View Ruby 1.8.7
Rehearsal ----------------------------------------------------------
via_case(nil) 0.260000 0.000000 0.260000 ( 0.296114)
via_literal(nil) 0.530000 0.000000 0.530000 ( 0.552677)
via_case("null") 0.750000 0.000000 0.750000 ( 0.761376)
via_literal("null") 0.570000 0.000000 0.570000 ( 0.646138)
via_case("nomatch") 1.610000 0.000000 1.610000 ( 1.669481)
via_literal("nomatch") 0.390000 0.010000 0.400000 ( 0.430214)
------------------------------------------------- total: 4.120000sec
user system total real
View gist:609458
module Trigo
[:sin, :cos, :tan].each do |trigo_fun|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__
def #{trigo_fun}(angle)
_trigo(:#{trigo_fun}, angle)
end
RUBY_EVAL
end
private
View gist:599605
## Original
def reverse_sql_order(order_query)
order_query.join(', ').split(',').collect { |s|
if s.match(/\s(asc|ASC)$/)
s.gsub(/\s(asc|ASC)$/, ' DESC')
elsif s.match(/\s(desc|DESC)$/)
s.gsub(/\s(desc|DESC)$/, ' ASC')
else
s + ' DESC'
end
View gist:559673
h = {}
v = 1
threads = Array.new(my_cpu_count = 4) do
Thread.new do
2_000_000.times {|i| h[i] = v}
end
end
threads.map(&:join)
You can’t perform that action at this time.