Skip to content

Instantly share code, notes, and snippets.

View mediocretes's full-sized avatar

Nathan D Acuff mediocretes

  • Expected Behavior
View GitHub Profile
#prone to cursor timeouts:
def old
thing.find(query, options).each do |item|
Enumerator.new do |y|
y << do_formatting_and_stuff(item)
end
end
end
#immune to cursor timeouts? the block form of find can be run with an infinite timeout cursor.
class Hash
def self.nmerge(*hashes, &block)
keys = hashes.map(&:keys).flatten.uniq
rhashes = hashes.reverse
keys.inject({ }) do |output, key|
if block
output[key] = block.call(key, hashes.map{ |x| x[key]})
else
rhashes.each do |hash|
if hash[key]
class Evens < Stage
def process
value = 0
loop do
output value
value += 2
end
end
end
Each.new('foo'){|x| x.chars} # 'f' 'o' 'o'
Each.new(['foo', 'bar']) | Each.new{|x| x.chars} # 'f' 'o' 'o' 'b' 'a' 'r'
module Cie
class Data
@logger = defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : SimpleLogger.instance
@wrappers = Hash.new
def self.connect(slave_ok)
puts "Cie::Data.connect!"
@logger.debug "Regenerating #{slave_ok ? '' : 'Writable '}Writable Mongo Connection #{CIE_CONNECTION.inspect}"
options = CIE_CONNECTION.merge(:slave_ok => slave_ok)
options[:logger] = @logger if options[:debug]
Mongo::Connection.new(options[:server], options[:port], options).db(CIE_CONNECTION[:db_name])
require 'Benchmark'
class OldFashioned
def self.call(i)
x = 2+i
end
end
class NewFangled
def self.call(i)
require 'simple_logger'
module Cie
class Data
@logger = defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER : SimpleLogger.instance
def self.connect(slave_ok)
options = CIE_CONNECTION.merge(:slave_ok => slave_ok)
Mongo::Connection.new(options[:server], options[:port], options).db(CIE_CONNECTION[:db_name])
end
# WORKS:
def retailer
ap 'fuck you ruby'
@my_retailer = Cie::RetailerSettings.find_by_name(name.gsub(/((C|N)m)?Controller/, '')) if @my_retailer.nil?
return @my_retailer
end
#DOES NOT WORK:
def retailer
class MiniTest::Unit
alias :run_test_suites_old :run_test_suites
def run_test_suites(filter = /./)
@test_count, @assertion_count = 0, 0
old_sync, @@out.sync = @@out.sync, true if @@out.respond_to? :sync=
TestCase.test_suites.each do |suite|
test_cases = suite.test_methods.grep(filter)
if test_cases.size > 0
@@out.print "\n#{suite}:\n"
end
Total movies 7645
Total actors 18973
Total movies with at least 2 actors 6778
Actors movie combinations 57187
bacon number 0 has 36
bacon number 1 has 1437
bacon number 2 has 5111
bacon number 3 has 566
bacon number 4 has 51
bacon number 5 has 13