Skip to content

Instantly share code, notes, and snippets.

Ilya Grigorik igrigorik

View GitHub Profile
View query.sql
SELECT * FROM
(SELECT 'desktop' type,
NTH(50, quantiles(bytesCSS)) median,
NTH(75, quantiles(bytesCSS)) seventy_fifth,
NTH(90, quantiles(bytesCSS)) ninetieth
FROM [httparchive:runs.latest_pages]),
(SELECT 'mobile' type,
NTH(50, quantiles(bytesCSS)) median,
NTH(75, quantiles(bytesCSS)) seventy_fifth,
NTH(90, quantiles(bytesCSS)) ninetieth
@igrigorik
igrigorik / mysql-var-stats.rb
Created Jan 22, 2009
replacement for mysqladmin ext -ri10
View mysql-var-stats.rb
# 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'
View gist:50363
#!/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 Jan 22, 2009
common math functions
View math-functions.rb
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
View ordered-hash.rb
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 }
View gist:60421
##
# 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
View eventmachine.markdown

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

View gist:79685
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
View gist:81593
# 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
View gist:107366
##
# 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
You can’t perform that action at this time.