Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View igrigorik's full-sized avatar
:octocat:

Ilya Grigorik igrigorik

:octocat:
View GitHub Profile
@igrigorik
igrigorik / mysql-var-stats.rb
Created January 22, 2009 00:53
replacement for mysqladmin ext -ri10
# Equivalent to mysqladmin ext -ri10
# - that is, if mysqladmin ext -ri10 works on your box
# - in my case it didn't...
#
# Reports MySQL variable changes for a certain time period
#
# Running: ruby mysql-var-stats.rb interval
require 'rubygems'
require 'ruport'
#!/usr/bin/env ruby
require 'rubygems'
require 'mini_magick'
BLANK = 'blank.png' # 1x1px white png
$text = 'hello, world' # text to write
img = MiniMagick::Image.from_file(BLANK)
@igrigorik
igrigorik / math-functions.rb
Created January 22, 2009 01:05
common math functions
class Array; def sum; inject( nil ) { |sum,x| sum ? sum+x : x }; end; end
class Array; def mean; self.sum/self.size.to_f; end; end
class Array; def variance; mean = self.mean; Math.sqrt(inject( nil ) { |var,x| var ? var+((x-mean)**2) : ((x-mean)**2)}/self.size.to_f); end; end
# inputs a random variable, sets mean = 0 and variance = 1
def standardize_random_variable(x)
mean = x.mean
variance = x.variance
x.map!{|a| (a-mean)/variance }
end
require 'rubygems'
require 'benchmark'
require 'facets/dictionary'
require 'rbtree'
n = 1000000
Benchmark.bm do |x|
@h = Hash.new
x.report("Hash insert") { n.times do @h[rand(n)] = 1 end }
x.report("Hash access") { n.times do @h[rand(n)] end }
@igrigorik
igrigorik / gist:60421
Created February 8, 2009 16:42 — forked from tmm1/gist:58437
##
# sync api: return values and exceptions
begin
images = []
results = RestClient.get('http://google.com/search?q=ruby')
Hpricot(results).find('a').each{ |link|
page = RestClient.get(link)
begin
require "rubygems"
require "tokyocabinet"
require "benchmark"
include TokyoCabinet
records = 1000000
hdb = HDB::new # Hash database; acts as a key value store
hdb.open("casket.hdb", HDB::OWRITER | HDB::OCREAT)

EventMachine Notes

Adapted to Markdown format from rom Aman Gupta's gist at: http://gist.github.com/79224

Using:

require 'rubygems'
require 'eventmachine'

EM::run Takes over the Process

require "rubygems"
require "curb"
module Curl
class Easy
def self.fetch(req, &blk)
curl = Curl::Easy.new(req['url'], &blk)
curl.follow_location = true
curl.max_redirects = 3
# async_sinatra_example.ru
require 'sinatra'
# Normally Sinatra::Base expects that the completion of a request is
# determined by the block exiting, and returning a value for the body.
#
# In an async environment, we want to tell the webserver that we're not going
# to provide a response now, but some time in the future.
#
# The a* methods provide a method for doing this, by informing the server of
##
# sync api: return values and exceptions
begin
images = []
results = RestClient.get('http://google.com/search?q=ruby')
Hpricot(results).find('a').each{ |link|
page = RestClient.get(link)
begin