This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'activesupport' | |
class Object | |
# As popularized by Why: | |
# http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html | |
# | |
def metaclass | |
class << self; return self; end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'minitest/unit' | |
require 'minitest/spec' | |
MiniTest::Unit.autorun | |
# This walks the given stub strings and sets up partial stub responder chains. | |
# | |
class StubBuilder | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Floppy | |
def method_missing(method, *args) | |
super unless args.length > 0 && method.to_s[-1..-1] == "=" | |
if args.first.is_a?(Proc) | |
(class << self; self; end).class_eval do | |
define_method(method.to_s[0..-2].to_sym, args.first) | |
end | |
else | |
(class << self; self; end).send(:attr_accessor, method.to_s[0...-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LazyProxy | |
instance_methods.each do |method| | |
undef_method(method) if method !~ /^__/ | |
end | |
def initialize &lazy_proxy_block | |
@lazy_proxy_block = lazy_proxy_block | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
def self.inherited inheriter | |
class << inheriter | |
def inherited inheriter | |
Object.const_set inheriter.name.to_sym, self | |
p "Sorry, A can't have grandchildren. You, #{inheriter}, are instead to be of class #{self}." | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Say, you want only to use the latest arriving ajax request. | |
// For example in a search engine, where only the request of the | |
// most recently sent request is of interest. Responses that come | |
// after and have been sent before the one that has already arrived | |
// are ignored: | |
// remember the latest request date | |
// | |
var latestRequestDate = new Date(); | |
function responseHandler() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; lisp | |
(defmacro x-injector () | |
'x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#; named lambda klass | |
#; in an environment with counter set to 0 | |
#; define an anonymous lambda which increases the counter by one | |
# | |
# (defun klass () | |
# (let ((counter 0)) | |
# (lambda () (incf counter)))) | |
;klass = lambda { | |
; counter = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In your unicorn.rb | |
# | |
after_fork do |server, worker| | |
GC.disable | |
end | |
# The rack app. | |
# | |
Responder = lambda do |env| | |
harakiri |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
# Note: Could also be called ary_join. | |
# | |
def real_join element | |
combined = [element]*(size-1) | |
zip(combined).flatten.compact | |
end | |
end | |
p [1,2,3,4].real_join 'and' #=> [1, 'and', 2, 'and', 3, 'and', 4] |
OlderNewer