Skip to content

Instantly share code, notes, and snippets.

View avdi's full-sized avatar

Avdi Grimm avdi

View GitHub Profile
@avdi
avdi / exception_tester.rb
Created January 10, 2011 04:28
A proof of concept for exception testing in Ruby
require 'set'
class ExceptionTester
class TestException < Exception
end
# Accepts a block containing the code you want to make exception safety
# assertions about.
def initialize(&exercise)
@exercise = exercise
function foo() {
var a = [];
for(var i = 0; i < 10; ++i) {
a[i] = function() {
console.log("The number is: " + i);
};
}
a[6]();
}
DataMapper::RepositoryNotSetupError: Adapter not set: default. Did you forget to setup?
/home/avdi/.rvm/gems/ruby-1.9.2-p0@bench/gems/dm-core-1.1.0/lib/dm-core/repository.rb:72:in `adapter'
/home/avdi/.rvm/gems/ruby-1.9.2-p0@bench/gems/dm-core-1.1.0/lib/dm-core/model/property.rb:160:in `field_naming_convention'
/home/avdi/.rvm/gems/ruby-1.9.2-p0@bench/gems/dm-core-1.1.0/lib/dm-core/property.rb:494:in `field'
/home/avdi/.rvm/gems/ruby-1.9.2-p0@bench/gems/dm-core-1.1.0/lib/dm-core/model.rb:567:in `block in load'
/home/avdi/.rvm/gems/ruby-1.9.2-p0@bench/gems/dm-core-1.1.0/lib/dm-core/model.rb:567:in `map'
/home/avdi/.rvm/gems/ruby-1.9.2-p0@bench/gems/dm-core-1.1.0/lib/dm-core/model.rb:567:in `load'
/home/avdi/.rvm/gems/ruby-1.9.2-p0@bench/gems/dm-core-1.1.0/lib/dm-core/repository.rb:162:in `read'
/home/avdi/.rvm/gems/ruby-1.9.2-p0@bench/gems/dm-core-1.1.0/lib/dm-core/collection.rb:1117:in `lazy_load'
/home/avdi/.rvm/gems/ruby-1.9.2-p0@bench/gems/dm-core-1.1.0/lib/dm-core/support/lazy_array.rb:275:in `to_a'
@avdi
avdi / gist:1026546
Created June 15, 2011 05:45
A Guardfile for starting Redis
module ::Guard
class Redis < Guard
def start
puts "Starting Redis on port #{port}"
IO.popen("#{executable} -", 'w+') do |server|
server.write(config)
server.close_write
end
puts "Redis is running with PID #{pid}"
$?.success?
@avdi
avdi / emacs-pidfile.el
Created June 17, 2011 19:36
Write pidfile on emacs-server startup
(setq pidfile "emacs-server.pid")
(add-hook 'emacs-startup-hook
(lambda ()
(with-temp-file pidfile
(insert (number-to-string (emacs-pid))))))
(add-hook 'kill-emacs-hook
(lambda ()
(when (file-exists-p pidfile)
(delete-file pidfile))))
require 'spec_helper'
describe Feeds::Drops::Rss::EntryDrop do
use_vcr_cassette
subject {
Feeds::Drops::Rss::EntryDrop.new(rss_app.feed_entries_to_display[1], context)
}
let(:context) { mock('context') }
let(:rss_app) {
@avdi
avdi / deep_fetch.rb
Created June 28, 2011 19:28
Deep Fetch
module DeepFetch
def deep_fetch(*keys, &fetch_default)
throw_fetch_default = fetch_default && lambda {|key, coll|
args = [key, coll]
# only provide extra block args if requested
args = args.slice(0, fetch_default.arity) if fetch_default.arity >= 0
# If we need the default, we need to stop processing the loop immediately
throw :df_value, fetch_default.call(*args)
}
catch(:df_value){
@avdi
avdi / throw-catch.rb
Created July 11, 2011 05:55
throw/catch demo code for RubyLearning article
require 'rubygems'
require 'mechanize'
MAX_PAGES = 6
def each_google_result_page(query, max_pages=MAX_PAGES)
i = 0
a = Mechanize.new do |a|
a.get('http://google.com/') do |page|
search_result = page.form_with(:name => 'f') do |search|
@avdi
avdi / defined.rb
Created July 26, 2011 04:23
Why to use `if defined?(@foo)` instead of `if @foo`
# -*- coding: utf-8 -*-
warn "Checking truthiness"
if @foo
# ...
end
warn "Done\n"
warn "Checking definedness and then truthiness"
if defined?(@foo) && @foo
# ...
@avdi
avdi / page_number.rb
Created August 3, 2011 19:49
PageNumber
require 'delegate'
# A constrained integer class representing Page Numbers
class PageNumber < DelegateClass(Integer)
BIGINT = 9223372036854775807
def initialize(value, name)
value = value.to_i
if 'offset' == name ? (value < 0 or value > BIGINT) : value < 1
raise RangeError, "invalid #{name}: #{value.inspect}"