This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'thread' | |
require 'delegate' | |
class SizedStack < DelegateClass(SizedQueue) | |
def initialize(size) | |
super(SizedQueue.new(size)) | |
end | |
def shift | |
__getobj__.pop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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) | |
{ |