Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
@ryanlecompte
ryanlecompte / gist:3816414
Created October 2, 2012 05:38
Shallow file finder
require 'pathname'
# My answer to stackoverflow question posted here:
# http://stackoverflow.com/questions/12684736/a-twist-on-directory-walking-in-ruby
class ShallowFinder
def initialize(root)
@matches = {}
@root = Pathname(root)
end
@ryanlecompte
ryanlecompte / gist:3790424
Created September 26, 2012 20:33
an alternative resque SizedStack that doesn't suck
require 'thread'
require 'delegate'
class SizedStack < DelegateClass(SizedQueue)
def initialize(size)
super(SizedQueue.new(size))
end
def shift
__getobj__.pop
@ryanlecompte
ryanlecompte / gist:3631742
Created September 5, 2012 06:33
My solution to New Relic's programming challenge
# I had a lot of fun doing this one! The following code makes
# the tests at https://gist.github.com/6ea0a0ba5702824075ab pass.
#
# NOTE: I normally would DRY some of this code up, but it's just a
# fun challenge and would never be deployed to production. :)
module MethodInstrumenter
def self.instrument_path(path)
@path = path
@counts = 0
@ryanlecompte
ryanlecompte / gist:3422573
Created August 22, 2012 05:38
Hash#smash
class Hash
def smash(prefix = nil)
inject({}) do |acc, (k, v)|
key = prefix.to_s + k
if Hash === v
acc.merge(v.smash(key + '-'))
else
acc.merge(key => v)
end
end
@ryanlecompte
ryanlecompte / gist:3342657
Created August 13, 2012 17:34
Using google_visualr for building generic charts
require 'google_visualr'
class ChartBuilder
# Creates a new builder instance.
def initialize(name, options = {})
@name = name
@columns = options.fetch(:columns) { raise 'No columns defined' }
@chart = options.fetch(:chart) { raise 'No chart class defined' }
@chart_options = {:name => @name}.merge(options.fetch(:chart_options, {}))
@rows = []
@ryanlecompte
ryanlecompte / gist:3297514
Created August 8, 2012 18:52
AR Memory Leak #2
require_relative 'config/environment'
10.times do
Thread.new do
loop do
Event.uncached do
Event.where("created_at >= '2012-08-07'").all.take(100)
end
end
end
@ryanlecompte
ryanlecompte / gist:3281509
Created August 7, 2012 04:16
ActiveRecord memory leak with multiple threads?
# It appears that when I perform a query with AR via multiple threads,
# the instantiated objects do not get released when a GC is performed.
threads = Array.new(5) { Thread.new { Foo.where(:status => 2).all.first(100).each { |f| f.owner.first_name } } }
threads.each(&:join)
threads = nil
GC.start
ObjectSpace.each_object(Foo).count # => instances still exist
[1] pry(main)> class A
[1] pry(main)* def foo
[1] pry(main)* end
[1] pry(main)* alias_method :foo2, :foo
[1] pry(main)* end
=> A
[2] pry(main)> m1 = A.instance_method(:foo)
=> #<UnboundMethod: A#foo>
[3] pry(main)> m2 = A.instance_method(:foo2)
=> #<UnboundMethod: A#foo>
class Foo
def foo; "foo" end
def bar; "bar" end
def baz; "baz" end
end
class Bar < Foo
attr_accessor :plural
def self.wrap(*methods)
[1] pry(main)> show-method Array#dup
From: object.c in Ruby Core (C Method):
Number of lines: 14
Owner: Kernel
Visibility: public
VALUE
rb_obj_dup(VALUE obj)
{