Skip to content

Instantly share code, notes, and snippets.

# A possibly misguided attempt to introduce automatic operator commutativity to Ruby. Say
# I add a '+' method to Range so that for example:
#
# (3..7) + 12 #=> (15..19)
#
# I'd expect to be able to call '12 + (3..7)' and get the same answer. This means I need
# to override Fixnum#+, meaning my new addition operator is expressed in two places. This
# module generates methods that automatically fill in the gaps so you only need to express
# the semantics of an operator in a single method, in a single class.
#
class Module
# Inspect the method lookup process like you can in JS.Class
# http://jsclass.jcoglan.com/reflection.html
#
# Array.lookup :any?
# #=> [#<UnboundMethod: Enumerable#any?>]
#
# class Array; def any?; super; end; end
# Array.lookup :any?
# #=> [#<UnboundMethod: Array#any?>, #<UnboundMethod: Enumerable#any?>]
# Make Ruby Hashes look more like JavaScript objects. Uses
# instance_exec so you can store lambdas on the object and
# call them like methods
#
# o = {}
# o.foo = 'foostring'
# o.bar = lambda { |s| s + foo }
#
# o.bar('barstring') #=> 'barstringfoostring'
# Subclass of Module, used for storing methods copied from other
# modules and classes. This lets classes shuffle their methods off
# into a module to we can place other modules in front of the
# class's own methods
class StashModule < Module
class << self
def [](id)
@modules[id]
end
Function.prototype.diff = function(eps) {
var fn = this, eps = eps || 1e-6;
return function(x) {
return (fn(x+eps) - fn(x)) / eps;
};
};
(function(x) { return x*x }).diff()(6)
// -> 12
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# don't put duplicate lines in the history. See bash(1) for more options
# don't overwrite GNU Midnight Commander's setting of `ignorespace'.
export HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups
[user]
name = James Coglan
email = jcoglan@googlemail.com
[alias]
ci = commit -a
co = checkout
st = status
sta = status -a
praise = blame
class Module
def include_no_hooks(mod)
include Module.new { include mod }
end
def extend_no_hooks(mod)
extend Module.new { include mod }
end
# Stripped-down version of Consent's expression language
# http://blog.jcoglan.com/2009/01/07/writing-your-own-expression-language-in-ruby/
# http://blog.jcoglan.com/2009/01/16/and-now-the-rules/
require 'observer'
module Consent
class Expression
def initialize(controller, params = {})
# Make up for Ruby's odd lack of support for blocks as observers
#
# class Foo
# include Observable::Blocks
# end
#
# f = Foo.new
# f.add_observer { puts "f changed" }
require 'observer'