Skip to content

Instantly share code, notes, and snippets.

View ryanlecompte's full-sized avatar

Ryan LeCompte ryanlecompte

View GitHub Profile
@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
@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: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
@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.

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
@technoweenie
technoweenie / dump_cotweet.rb
Created February 3, 2012 00:09
export cotweet sent timeline
require 'json'
require 'fileutils'
require 'faraday'
email, pass, dir = ARGV
if !(email && pass)
puts "Pass your CoTweet email/password:"
puts
puts " $ ruby #{$0} [email protected] sekretpassword [/path/to/dump]"
@myobie
myobie / mountain-lion-brew-setup.markdown
Created February 18, 2012 20:14
Get Mountain Lion and Homebrew to Be Happy

Get Mountain Lion and Homebrew to Be Happy

1) Install XCode 4.4 into /Applications

Get it from the App Store.

2) Install Command Line Tools

In XCode's Preferences > Downloads you can install command line tools.

@masterkain
masterkain / Gemfile
Created March 1, 2012 03:29
Sidekiq GitHub issue reporter
gem 'sidekiq'
gem 'octokit'
(The MIT License)
Copyright (c) 2012-2020 Myron Marston
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
require 'thread'
class Worker
def initialize(count = 1)
@queue, @closing, @threads, @mutex = Queue.new, false, [], Mutex.new
add_worker(count)
end
def add_worker(count = 1)
@mutex.synchronize do