Skip to content

Instantly share code, notes, and snippets.

class Module
def subclasses
classes = []
ObjectSpace.each_object do |klass|
next unless Module === klass
classes << klass if self > klass
end
classes
end
end
var Globals = {
originals: [],
userDefined: [],
warned: [],
root: this,
initialize: function() {
if (this.originals.length > 0) return;
for (var key in this.root) this.originals.push(key);
},
class Module
method = instance_method(:instance_methods)
define_method :instance_methods do |condition = true|
return method.bind(self).call(condition) unless Module === condition
anc = ancestors
index = anc.index(condition) || -1
anc[0..index].inject([]) { |methods, ancestor|
methods.concat(ancestor.instance_methods(false))
}.uniq
end
// Converts any function to accept continuation-passing style
//
// var add = function(a, b) {
// return a + b;
// };
//
// var addcps = add.toCPS();
//
// addcps(3, 4, function(x) { alert(x) });
# Allows methods to be modified so that they accept continuation-passing
# style, passing their return value to a block instead of returning it
# directly
module Continuations
METHODS = {}
def accept_continuation(*args)
args.each do |method_name|
func = instance_method(method_name)
// Turns any function into a constructor that will work
// with or without the 'new' keyword. The returned function
// shares its prototype with the source function for
// transparency.
Function.prototype.toClass = function() {
var func = this, bridge = function() {};
bridge.prototype = func.prototype;
var make = function(args) {
// Causes Ojay.HTML to add type-matching class names
// to all generated input tags
Ojay.HtmlBuilder.include({
input: Ojay.HTML.input.wrap(function() {
var args = Array.from(arguments),
input = args.shift().apply(null, args);
Ojay(input).addClass(input.type);
return input;
})
// Provides Ruby-style scoping to JS.Class, whereby constants
// and nested classes are accessible as 'this.FOO' within class
// and instance methods, removing the need for extend() blocks
// and 'this.klass.FOO' references.
JS.RubyScoping = new JS.Module({
extend: {
included: function(base) {
// Store constants in a separate module, and have
// base inherit its constants from this
%w(+ -).each do |symbol|
Range.class_eval do
define_method symbol do |value|
x, y = [first, last].map { |x| x.send(symbol, value) }
x..y
end
end
end
%w(* / **).each do |symbol|
data = ARGV.dup
name, scores = data.shift, data.map { |n| n.to_i }
# Group scores into frames
frames = scores.inject([]) do |table, score|
previous = table.last
frame = previous || [0]
frame = [0] if frame.size == 3 or frame[1] == 10
table << frame unless frame == previous