Skip to content

Instantly share code, notes, and snippets.

@trans
trans / yaml_date_load_bug.rb
Created July 13, 2010 11:22
Where is date.rb coming from?
module Kernel
alias_method :old_require, :require
alias_method :old_load, :load
alias_method :old_autoload, :autoload
def require(path)
p "require: #{path}"
old_require(path)
end
#!/usr/bin/ruby
require 'cgi'
__DIR__ = File.dirname(__FILE__)
HOME = 'HomePage'
LINK = 'wiki.cgi?name=%s'
query = CGI.new 'html4'
@trans
trans / module_vs_binding.rb
Created June 19, 2010 20:55
Compare Module to Binding Eval
class S < Module
def initialize
extend self
end
def __binding__
@binding ||= binding
end
class String
# Returns short abstract of long strings; not exceeding +range+
# characters. If range is an integer then the minimum is 20%
# of the maximum. The string is chopped at the nearest word
# if possible, and appended by +ellipsis+, which defaults
# to '...'.
#
# CREDIT: George Moschovitis, Trans
@trans
trans / eventhook.rb
Created June 5, 2010 02:18
Exception-based Event Hooks
# = Exception-based Event Hooks
#
# Provides an Event Hooks system designed
# on top of Ruby's built-in Exception system.
#
# def dothis2
# puts 'pre'
# hook :quitit
# puts 'post'
# end
class VersionResolver
attr :dependencies
attr :versions
attr :resolution
# dependencies - Runtime dependencies in the form of a list of
# [gemname, constraint1, constraint2, ...]
def initialize(dependencies)
@dependencies = dependencies
@trans
trans / function.rb
Created April 17, 2010 13:29
function.rb
# Function is an experiment in unifying Method, UnboundMethod and Proc.
class Function
#
# MODES block Proc.new ? lambda
# +----------------------------------
# @pcheck | false true false true
# @rlocal | false false true true
#
@trans
trans / hooks.rb
Created April 17, 2010 13:11
hooks.rb
# Simple hook/signal system.
#
# TODO: hooks should be an inheritor
#
module Hook
def self.append_features(base)
base.extend self
end
@trans
trans / behavior.rb
Created April 17, 2010 12:28
behavior.rb
# Create temporary extensions.
# Based on original code (c)2005 Nobuyoshi Nakada
# = Beahvior
#
# Behavior class is used to encapsulates a behavior.
#
# nil.behaving(:to_nil) do
# "1"
# end
@trans
trans / openproxy.rb
Created April 17, 2010 10:52
openproxy.rb
# Include Open into a class and it automatically becomes proxied.
require 'meta'
module Open
def self.included( base )
( class << base ; self ; end ).class_eval {
alias_method :create, :new
define_method( :new ) { |*args|
OpenProxy.new( base, *args )