Skip to content

Instantly share code, notes, and snippets.

View vincentchu's full-sized avatar

Vincent Chu vincentchu

View GitHub Profile
@vincentchu
vincentchu / smith-waterman.rb
Created June 23, 2011 05:50
Smith Waterman implementation in Ruby
class Matrix
IndexOverflow = Class.new(StandardError)
attr_reader :dims, :nrows, :ncols
def initialize(mm, nn)
@dims = [mm, nn]
@nrows = mm
@ncols = nn
@vincentchu
vincentchu / test_server.rb
Created May 28, 2011 05:02
Test sinatra + EventMachine
require 'rubygems'
require 'sinatra/base'
require 'eventmachine'
require 'logger'
$logger = Logger.new(File.join(File.dirname(__FILE__), "development.log"))
module TestEM
def self.start
if defined?(PhusionPassenger)
(`·. )\ )`·. )`·.
) `·. .·´( .·´ ( (`·. )`·._.·´( )`·. /( .·´ ( /( .·´ (
.·´( .·´:..::(,(::-- ' '\:·´ \::`·._)`·. )\.·´::... .::) .·´ ./ )\ ) `·._):::. ) )\ ) `·._):::. )
);; :-- ' ' _\ .·´( )::. ..:::).·´;· -- ´ ´\::.`·.(::...:(' )\ .·´ .:`·.(:;; -- ' '\:. :(.·´) )\ )\ .·´ .:`·.(:;; -- ' '\:. :(.·´) )\
.·´/\ ,..:´:::'/ ):..\(;;::-- ´ ´ '\:::::::...::) .·´ (,): -- ' ' \....:::`·.( ( .·´ (,): -- ' ' \....:::`·.( (
)/:::'\...:/I I::::::::/ '(::...:/\
@vincentchu
vincentchu / pop_quiz_asshole.rb
Created May 18, 2011 22:24
pop_quiz_asshole.rb
begin
raise "THIS IS WEIRD?"
rescue ex
puts "EX = #{ex.message}"
end
# => undefined local variable or method `ex' for main:Object (NameError)
@vincentchu
vincentchu / histogram.rb
Created May 18, 2011 02:39
histogram.rb
#!/usr/bin/ruby
binwidth = ARGV[0].to_f
filename = ARGV[1]
rand_file_name = "rand_file-#{(rand*1000000).to_i}.data"
nums = File.open(filename, "r") {|f| f.readlines}.collect {|n| n.to_f }
binned_data = {}
nums.each do |n|
require 'rubygems'
require 'redis'
require 'redis/distributed'
redis = Redis::Distributed.new(['redis://localhost:6379', 'redis://localhost:6380'])
(1..10_000).each do |i|
key = "rand_#{rand}"
val = rand(1000)
@vincentchu
vincentchu / random_numbers_and_forking.rb
Created March 20, 2011 03:38
Using Ruby 1.8.7, let's take a look at how the random number generator behaves under forking
[false, true].each do |reseed_rand|
puts "*" * 100
puts "Force reseed of the random number generator? #{reseed_rand}"
2.times {
fork {
srand if reseed_rand
puts sprintf(" Process ID: %5d, child of %5d: 3 Randoms: %.5f, %.5f, %.5f", Process.pid, Process.ppid, rand, rand, rand)
}
}
--------------- objects --------------- ------ pages ------ ----- memory -----
load-in swap-out swapped delta used delta used delta
28 0 3742556 -43 10977293 -117 3.45G +101.54K
28 0 3742517 -39 10977207 -86 3.45G +56.30K
37 0 3742473 -44 10977107 -100 3.45G +65.12K
29 0 3742433 -40 10976982 -125 3.45G +116.73K
31 0 3742391 -42 10976850 -132 3.45G +78.88K
30 0 3742351 -40 10976779 -71 3.45G +50.25K
29 0 3742315 -36 10976673 -106 3.45G +426.68K
30 0 3742271 -44 10976552 -121 3.45G +183.50K
@vincentchu
vincentchu / redis.conf
Created March 19, 2011 19:59
Redis Conf
daemonize yes
pidfile /var/run/redis.pid
port 6379
timeout 30
loglevel notice
logfile /var/log/redis/redis-server.log
databases 16
################################ SNAPSHOTTING #################################
save 300 1000
@vincentchu
vincentchu / redis_thread_safe.rb
Created March 9, 2011 19:31
thread safety issues
require 'rubygems'
require 'redis'
def random_redis
100.times do
rand_key = "key_#{rand(100)}"
rand_val = rand
if (rand_val < 0.33)