Skip to content

Instantly share code, notes, and snippets.

@trans
trans / testmeth.rb
Created June 24, 2009 11:52
TestCase Shortcut
# Create test/unit test with testcase("xxx") { test("yyy") { ... } ... } notation.
# Out-dated considering things like Shoulda.
# (c)2003 Christian Neukirchen
#
# Has one small issue:
# There is no way to close off blocks from the outside scope.
# This can cause interference between separate tests.
# So while this works for simple cases, it may well run into
# problems with anything sizable.
@trans
trans / trulyprivate.rb
Created June 24, 2009 12:53
True Public/Private Access
# Playing around with an idea for true public vs. private access.
#
# Y is a subclass of X.
# x is an instance of X (ie. a public interface)
# Likewise y is an instance of Y.
#
# y
# |
# V
# Y:private -> Y:protected -> Y:public x
@trans
trans / chain.rb
Created June 24, 2009 13:37
Method Chaining Concept
# Experimental library for safe fluent method chaining.
# (Interesting idea, but a Null class would be better.)
#
# person = "John Doe"
# def person.address = "123 West St."
# person.address.street #=> Error
#
# To avoid the error use #chain:
#
# person.chain.address.street.end #=> nil
@trans
trans / method_filter.rb
Created June 24, 2009 14:09
Dynamically Privatize Methods
# Method Filter - Dynamically Privatize New Methods
#
# Since Ruby is very dynamic, methods added to Object will show
# up in the list of available methods of subclasses. If we are trying to privatize
# methods to get them out of the way for method_missing, then this could pose
# a problem. We handle this by defining hooks in Object, Kernel and Module
# that will hide any method so defined automatically.
#
# This is rather useless now that Ruby 1.9 provides BasicObject.
@trans
trans / module_clone.rb
Created June 24, 2009 15:01
Module Clone Methods
# Module#clone_xxx methods. These methods make it easy to
# create new subsets (or sidesets) of current modules.
# However facets/module pseudo-traits methods are better.
class Module
# Returns an anonymous module with the specified methods
# of the receiving module renamed.
def clone_renaming( pairs )
mod = self.dup
@trans
trans / memoize.rb
Created June 29, 2009 22:30
Simple Memoize Impementation
class Module
# Directive for making your functions faster by trading
# space for time. When you "memoize" a method/function
# its results are cached so that later calls with the
# same arguments returns results in the cache instead
# of recalculating them.
#
# class T
# def initialize(a)
# @a = a
@trans
trans / cache.rb
Created June 29, 2009 22:31
Another Memoize Implementation
module Kernel #:nodoc:
# Object#cache is essentially like Module#memoize except
# it can also be used on singleton/eigen methods.
# OTOH, memoize's implementation is arguably better for it's
# use of #bind instead of #alias. Eventually the two implmenations
# should be reconciled with a single implementation.
#
def cache m = nil
if m
(Module === self ? self : (class << self; self; end)).module_eval <<-code
@trans
trans / wxruby-lazy.rb
Created September 7, 2009 11:25
WxRuby Example
class View < ::Wx::Frame
def initialize
super(nil, -1, "My Application",
:style => Wx::DEFAULT_FRAME_STYLE | Wx::TAB_TRAVERSAL,
:size => [800,600]
)
setup_controls
setup_events
end
@trans
trans / pool.rb
Created September 9, 2009 02:15
Thread-safe Pool Class
# Pool - Thread Safe Object Store
# Copyright (c) 2004 George Moschovitis
#
# TODO: Could use the SizedQueue/Queue?
require 'thread'
require 'monitor'
# Generalized object pool implementation. Implemented as a thread
# safe stack. Exclusive locking is needed both for push and pop.
@trans
trans / dci1.rb
Created February 11, 2010 20:20
DCI Example
require 'anise'
require 'facets/functor'
# Roll base class.
class Role
def initialize(player)
@player = player
end
# should be public send ?
def method_missing(s, *a, &b)