Adapted to Markdown format from rom Aman Gupta's gist at: http://gist.github.com/79224
Using:
require 'rubygems'
require 'eventmachine'
| # 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) |
| 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 } |
| ## | |
| # 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) | |
Adapted to Markdown format from rom Aman Gupta's gist at: http://gist.github.com/79224
Using:
require 'rubygems'
require 'eventmachine'
| 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 |