Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
module Stubbed end
def stub_module(full_name, &block)
stub_class_or_module full_name, Module, &block
end
def stub_class(full_name, &block)
stub_class_or_module full_name, Class, &block
end
@burke
burke / 0-readme.md
Created January 27, 2012 13:44 — forked from funny-falcon/cumulative_performance.patch
ruby-1.9.3-p327 cumulative performance patch for rbenv

ruby-1.9.3-p327 cumulative performance patch for rbenv

This installs a patched ruby 1.9.3-p327 with various performance improvements and a backported COW-friendly GC, all courtesy of funny-falcon.

Requirements

You will also need a C Compiler. If you're on Linux, you probably already have one or know how to install one. On OS X, you should install XCode, and brew install autoconf using homebrew.

@ryanlecompte
ryanlecompte / gist:1619490
Created January 16, 2012 06:59
Experimenting with forking and unix sockets in Ruby
require 'base64'
require 'socket'
require 'fileutils'
# UnixSocketForker is an experiment of inter-process communication using
# plain unix sockets to communicate between forked processes and the
# parent process. This can also be done via IO.pipe. In this experiment,
# the jobs are simply random arrays whose sums are calculated in the forked
# worker processes.
class UnixSocketForker
@ryanlecompte
ryanlecompte / gist:1371212
Created November 16, 2011 20:12
Ways to find duplicates in an array
ary = [1,1,1,1,2,3,4,5,5,5,5,5,5,10,10,100]
# approach 1
ary.enum_for(:sort).with_object([]) { |(a,b), result| result << a if a == b; a <=> b}.uniq
# approach 2
ary.select { |e| ary.count(e) > 1 }.uniq
# approach 3 (from @apeiros)
ary.group_by {|e| e}.select { |k, v| v.size > 1 }.keys
@ryanlecompte
ryanlecompte / gist:1283413
Created October 13, 2011 04:50
Providing an ActiveRecord-like before_filter capability to arbitrary Ruby classes
# First the end result of what we want:
class Foo
before_hook :whoa
before_hook :amazing
def test
puts "This is kinda cool!"
end
@dshaw
dshaw / index.html
Created September 21, 2011 21:05 — forked from 3rd-Eden/index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form id="chat">
<fieldset id="blabla">
</fieldset>
@Burgestrand
Burgestrand / method_locator.rb
Created September 21, 2011 16:46
Method locator and ancestral heritage
require 'method_locator'
class A
def moo
"A"
end
end
class B < A
def moo
@jcasimir
jcasimir / performance.markdown
Created September 21, 2011 13:27
Measuring Performance

Measuring Performance

Performance is often ignored in Rails development until it becomes a problem. If ignored too long, though, it can get very tricky to improve. It's valuable to regularly audit performance and look for hotspots or design choices that are slowing things down.

Inspecting the Logs

Inspecting the log will help identify the source of several performance issues the application may have.

The Rails application log outputs the time spent processing each request. It breakdowns the time spent at the database level as well processing the view code. In development mode, the logs are displayed on STDOUT where the server is being run. In a production setting the logs will be in log/production.log within the application's root directory.

@dcuddeback
dcuddeback / after_commit_with_transactional_fixtures.rb
Created August 25, 2011 01:36
Patch ActiveRecord to fire after_commit callbacks at the appropriate time during tests with transactional fixtures.
module ActiveRecord
module ConnectionAdapters
module DatabaseStatements
#
# Run the normal transaction method; when it's done, check to see if there
# is exactly one open transaction. If so, that's the transactional
# fixtures transaction; from the model's standpoint, the completed
# transaction is the real deal. Send commit callbacks to models.
#
# If the transaction block raises a Rollback, we need to know, so we don't
module Enumerable
def threaded_each(n = 30, &block)
each_slice(n) do |elems|
elems.map { |o| Thread.new(o, &block) }.each(&:join)
end
end
end
(1..100_000).threaded_each do |n|
puts n