Skip to content

Instantly share code, notes, and snippets.

# http://yehudakatz.com/2010/02/21/ruby-is-not-a-callable-oriented-language/
module DoAsYouPlease
Object.send :include, self
def method_missing(name, *args, &block)
self.class.const_get(name).call(*args, &block)
end
end
Foo = proc { 42 }
#!/usr/bin/env ruby
def find_deps(cookbook_dir)
nel = Hash.new { |h, k| h[k] = [] }
Dir.glob("#{cookbook_dir}/*/").each do |r|
deps_for(r, nel)
end
nel
end
@playerconnect
playerconnect / backend_server_port.rb
Created June 2, 2010 22:36
Resolve thin backend server port from Rails controller
# request.port is unfortunately useless from within the app, as it
# does not reflect the backend bind port, but the frontend proxy.
# So we have to reverse lookup the PID to figure out our port.
def backend_server_port
Thread.current[:backend_server_port] ||= begin
logger.debug "Attempting to reverse-lookup server port from PID file"
port = nil
Dir["#{RAILS_ROOT}/tmp/pids/thin.*.pid"].each do |f|
pid = File.read(f).chomp.to_i
if pid == $$
# Include an anonymous module
#
# Useful for defining a class with a base module. So, instead of:
#
# class Foo
# module Base
# def bar
# # ...
# end
# end
@addywaddy
addywaddy / semi-colons.js
Created October 28, 2010 07:41
Sometimes semi-colons are indeed necessary in Javascript
function echo (str) {
return str
}
echo("Hello")
(function() {
echo("Goodbye")
})
@raws
raws / gist:663581
Created November 5, 2010 02:55
EventMachine cleverness to capture stderr
module EventMachine
class StderrHandler < EventMachine::Connection
def initialize(connection)
@connection = connection
end
def receive_data(data)
@connection.receive_stderr(data)
end
end
# git lye
# An attempt to make rebasing easy, by draining unnessarcy commit noise.
# No warranty, use at own risk.
# Author Hannes Tydén <hannes@tyden.name>
# Contributor Tobias Schmidt <ts@soundcloud.com>
# TODO
# - Don't sign off all commits are by current user.
@jed
jed / LICENSE.txt
Created May 10, 2011 16:04 — forked from 140bytes/LICENSE.txt
write contextual templates
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
;; jneira's solution to Transitive Closure
;; https://4clojure.com/problem/84
(fn [s]
(let [f #(for [[a b] % [c d] %
:when (= c b)] [a d])]
(->> s (iterate #(let [n (into % (f %))]
(when (not= % n) n)))
(take-while identity)
(last))))
@hyone
hyone / gist:1032985
Created June 18, 2011 10:47
4clojure #53 - Longest continuous increasing subsequence
;; hyone's solution to Longest Increasing Sub-Seq
;; https://4clojure.com/problem/53
(fn longest-inc-seq [coll]
(reduce #(let [len-a (count %1)
len-b (count %2)]
(if (and (> len-b 1) (> len-b len-a)) %2 %1))
[]
(reductions
(fn [xs y]