Skip to content

Instantly share code, notes, and snippets.

View blambeau's full-sized avatar

Bernard Lambeau blambeau

View GitHub Profile
@blambeau
blambeau / not_so_flexible_require
Created January 14, 2011 16:30
How [NOT] to require external dependencies
#
# This method allows requiring dependencies with some flexibility.
#
# Implemented algorithm makes greedy choices about the environment:
# 1. It first attempts a simple <code>Kernel.require(name)</code> before
# anything else (even bypassing version requirement)
# 2. If step 1 fails with a LoadError then it falls back requiring the
# gem with specified version (defaults to >= 0) and retries step 1.
# 3. If step 2 fails with a NameError, 'rubygems' are required and step
# 2 is retried.
@blambeau
blambeau / tutorial_d_in_ruby.rb
Created January 28, 2011 13:48
Relational algebra on apache logs, thanks to veritas
require 'veritas/physical/logs'
file = File.expand_path('../access.log', __FILE__)
LOGS = Veritas::Physical::Logs.new(file, [:apache, :combined])
# How many hits per page ??
(debug (summarize LOGS, :path, :count => (count '*')))
# What pages have not been found ??
NOT_FOUND = (restrict LOGS, ->(t){ t[:http_status].eq(404) })
@blambeau
blambeau / hello.coffee
Created February 17, 2011 10:50
An example of syntax
class Hello extends Brick
constructor: ->
# Cell for the current view
@current = new Cell
# View handler for #content
@content = new View
selector:
@blambeau
blambeau / gist:1172939
Created August 26, 2011 07:58
Sloppy and tidy in Viiite
# A benchmark pioneered by @tenderlove
# @see https://gist.github.com/1170106
class Sloppy
def sloppy; @sloppy; end
end
class Tidy
def initialize; @tidy = nil; end
def tidy; @tidy; end
@blambeau
blambeau / gist:1269769
Created October 7, 2011 08:28
Wlang dialect
require 'wlang'
$vars = []
dialect = WLang::dialect do
rule "#" do |parser, offset|
varname, reached = parser.parse(offset)
$vars << varname
["#{varname}", reached]
@blambeau
blambeau / Gemfile
Created October 18, 2011 17:39
Auto-update ruby script
gem "grit", :git => "git@github.com:blambeau/grit.git"
@blambeau
blambeau / destr.rb
Created November 22, 2011 09:00
Fun destructuring in Ruby 1.9
class Hash
def with(&block)
block.call *block.parameters.map{|x| self[x.last]}
end
end
# Example
h = {:name => "blambeau", :hobby => "ruby"}
@blambeau
blambeau / client.rb
Created November 22, 2011 09:15
Thin test
require 'http'
NB = 5
times = []
threads = (0..NB).map do |i|
Thread.new(i) {|nb|
t1 = Time.now
puts Http.get("http://127.0.0.1:3000")
t2 = Time.now
times[nb] = (t2-t1)
@blambeau
blambeau / Gemfile
Created December 14, 2011 10:54
jquery-sinatra-streaming
source "http://rubygems.org/"
gem "sinatra", "~> 1.3.0"
gem "thin"
@blambeau
blambeau / Gemfile
Created January 13, 2012 13:22
Inheritance with multiple Citrus grammars
source :rubygems
gem "citrus"
gem "rspec"