Skip to content

Instantly share code, notes, and snippets.

View floere's full-sized avatar
😁
Open Sourcing

Florian R. Hanke floere

😁
Open Sourcing
View GitHub Profile
require 'rubygems'
require 'activesupport'
class Object
# As popularized by Why:
# http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
#
def metaclass
class << self; return self; end
@floere
floere / ministubs.rb
Created January 28, 2010 13:29
ministubs, based on nikos ministubs
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
@floere
floere / floppy.rb
Created February 8, 2010 23:18 — forked from pjb3/floppy.rb
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])
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
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
// 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() {
; lisp
(defmacro x-injector ()
'x)
#; 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
# In your unicorn.rb
#
after_fork do |server, worker|
GC.disable
end
# The rack app.
#
Responder = lambda do |env|
harakiri
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]