Skip to content

Instantly share code, notes, and snippets.

# by Matt Sears
# http://www.mattsears.com/articles/2011/11/27/ruby-blocks-as-dynamic-callbacks
class Proc
def callback(callable, *args)
self === Class.new do
method_name = callable.to_sym
define_method(method_name) { |&block| block.nil? ? true : block.call(*args) }
define_method("#{method_name}?") { true }
def method_missing(method_name, *args, &block) false; end
#!/usr/bin/env ruby
require 'rubygems'
gem 'rack', '=0.9.1'
gem 'thin', '=1.0.0'
require 'sinatra'
get '/' do
content_type 'text/plain'
"Hello, world"
end
@groesser3
groesser3 / lifo_fifo.rb
Created November 5, 2010 07:05
simple LIFO, FIFO functions in ruby
require "test/unit"
class Array
def fifo
verbrauch = inject(0) {|sum, el| sum+=el.abs if el < 0; sum }
res = select{|el| el > 0}.inject([]) do |result, el|
if el > verbrauch
result << (el - verbrauch)
verbrauch = 0
else
@groesser3
groesser3 / lifo_fifo.coffee
Created November 5, 2010 06:59
Simple LIFO, FIFO Functions written in CoffeeScript
# ---------------------------------
# Simple LIFO, FIFO Functions
# written in CoffeeScript
# ---------------------------------
verbrauch = (arr, menge) ->
res= []
for el in arr
if el >= 0
if el > menge
@groesser3
groesser3 / fifo.rb
Created November 5, 2010 06:58
fifo
require "test/unit"
class Array
def fifo
verbrauch = inject(0) {|sum, el| sum+=el.abs if el < 0; sum }
res = select{|el| el > 0}.inject([]) do |result, el|
if el > verbrauch
result << (el - verbrauch)
verbrauch = 0
else