Skip to content

Instantly share code, notes, and snippets.

def pid_path
"~/tmp/pid/"
end
def write_pid(name)
`echo "#{Process.pid}" > #{pid_path}#{name}.pid`
`echo "#{Process.ppid}" > #{pid_path}#{name}.ppid`
end
def delete_pid(name)
# Include an anonymous module
#
# Useful for defining a class with a base module. So, instead of:
#
# class Foo
# module Base
# def bar
# # ...
# end
# end
class Array
def map_detect(&block)
result = nil
detect do |item|
result = block.call(item)
end
result
end
end
def scooby
`say #{yield}`
end
scooby do
"doo-be-doo-be-doo"
end
@fronx
fronx / scooby-doo.rb
Created September 24, 2010 14:40 — forked from awendt/scooby-doo.rb
def scooby
`say #{yield}`
end
scooby do
"be-doo-be-doo"
end
@fronx
fronx / sm3d.js
Created December 16, 2010 22:35
find the right stylesheet to modify
/*
* Author: Tiffany Conroy
* RE: http://twitter.com/#!/smashingmag/status/14792918153830400
*
* Lots of help from here:
* http://24ways.org/2010/intro-to-css-3d-transforms
* http://9elements.com/html5demos/matrix3d/
* http://www.eleqtriq.com/2010/05/css-3d-matrix-transformations/
*/
Enumerable.module_eval do
def with_index
index = -1
map { |item| [item, index += 1] }
end
end
['a','x','foo'].with_index.map { |a, index| a * index }
# => ["", "x", "foofoo"]
Array.class_eval do
def uniq_by(&block)
self & Hash[
*inject([]) do |memo, item|
memo << block.call(item) << item
end
].values
end
end
module Enumerable
def and
inject(true) { |memo, value| memo && value }
end
def or
inject(false) { |memo, value| memo || value }
end
end
class Class
def self.with_reader(name, &block)
Class.new do
attr_reader :"#{name}"
define_method :initialize do |value|
instance_variable_set("@#{name}", value)
end
class_eval(&block) if block_given?
end
end