Skip to content

Instantly share code, notes, and snippets.

# Fast nested constant implementation
class FastNestedConstant : Fancy AST NestedConstant {
def initialize: @line string: @string {
names = @string split: "::"
@toplevel = false
if: (@string =~ /^::/) then: { @toplevel = true; names shift }
@names = names map: |n| { n to_sym }
}
def bytecode: g {
class Module {
forwards_unary_ruby_methods
def [constant_name] {
"""
@constant_name Name (@String@) of constant's name.
@return @constant_name's value.
Returns the value of the constant with the given name in @self.
"""
@dirk
dirk / state_machine.rb
Last active December 17, 2015 05:38
The only state machine I'll (probably) ever need.
class StateMachine
def initialize
@transitions = {}
end
def transition(opts, &block)
from = opts[:from]
to = opts[:to]
@transitions[from] ||= {}
@transitions[from][to] = block
end
@dirk
dirk / download-latest-chromium.rb
Created April 4, 2013 22:16
Download the latest Mac build of Chromium from their CI repo
#!/usr/bin/env ruby
version = `wget -O - -q http://commondatastorage.googleapis.com/chromium-browser-continuous/Mac/LAST_CHANGE`.strip
puts "Downloading Chromium continuous build version: #{version}"
STDOUT.flush
`wget -O "chromium-mac-continuous-#{version}.zip" http://commondatastorage.googleapis.com/chromium-browser-continuous/Mac/#{version}/chrome-mac.zip`
@dirk
dirk / environment.rb
Last active December 15, 2015 12:59
Fix for bug in Rails 2.3's vendored BlankSlate.
config.gem "toml", :version => "0.0.4"
class BlankSlate
class << self
# Hide the method named +name+ in the BlankSlate class. Don't
# hide +instance_eval+ or any method beginning with "__".
def hide(name)
# CHANGED: if instance_methods.include?(name.to_s) and
if instance_methods.include?(name.to_sym) and
name !~ /^(__|instance_eval)/
@hidden_methods ||= {}
@dirk
dirk / conway.rs
Created June 28, 2012 21:18
Conway's game of life in Rust
use std;
use time;
import result::{ok, err};
import to_str::*;
// Count the number of live neighbors for a given cell.
// Neighbors are cells adjacent vertically, horizontally, or diagonally.
fn live_neighbors(board: [[bool]], row: int, column: int) -> int {
syntax = r'''
<ts> := [ \t]*
# Variable delcarations
local_var := [a-z], [a-z0-9_]*
global_var := [A-Z], [a-z0-9_]+
# The chain of indexes and properties for objects
tail := (index/property/func_call)+
index := "[", expression, "]"
// Copyright (C) 2011 by Dirk Gadsden
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
class SpitefulStore
# A very mean cache store that loses everything you give it.
def read(key)
nil
end
def write(key, value)
value
end
end
@dirk
dirk / 2lines.rb
Created March 11, 2011 00:01
Sinatra in just two lines of code.
# This is a light-hearted riposte to rkh's great almost-sinatra (https://github.com/rkh/almost-sinatra) project.
%w{rubygems sinatra}.each {|g| require g }
get '/' do '<html><head><title>Definitely Sinatra</title></head><body>Hello World</body></html>'; end