Skip to content
View planets.rb
# initial inspiration
# pictures of output
require 'chunky_png'
Point = :x, :y do
def +(point) x+point.x, y+point.y
def *(n)
View recursive_sequential.rb
# Playing with the idea of continuations to allow a program to be written recursively,
# but accessed sequentially (I'm assuming enumerator uses Fibers underneath,
# but even if not, it's good enough)
class AST
def self.for(*attrs, &inspection) {
attr_reader *attrs
define_method(:initialize) do |*values| { |name, value| instance_variable_set "@#{name}", value }
View my_iterator.js
let MyIterator = function(n) {
this.n = n;
MyIterator.prototype[Symbol.iterator] = function() {
var i = -1;
var n = this.n;
var done = false;
return {
View math_parser.rb
# Was able to make this after watching
# I've tried various things like this before, this is probably my 20+ attempt
# (example
# I've succeeded at using Treetop previously, and Michael Baker helped me make one back in Chicago
# but this is the first one I made from scratch and succeeded at
module MathParser
module Parsers
class Parser
attr_reader :name
View active_record_self_join_table_with_mismatched_names.rb
require 'stringio'
def capture_stdout
real_stdout = $stdout.dup
read, write = IO.pipe
$stdout.reopen write
$stdout.reopen real_stdout

We love comfort. We love state-of-the-art practice facilities, oak-paneled corner offices, spotless locker rooms, and fluffy towels. Which is a shame, because luxury is a motivational narcotic: It signals our unconscious minds to give less effort. It whispers, Relax, you’ve made it. The talent hotbeds are not luxurious. In fact, they are so much the opposite that they are sometimes called chicken-wire Harvards. Top music camps—especially ones that can afford better—consist mainly of rundown cabins. The North Baltimore Aquatic Club, which produced Michael Phelps and four other Olympic medalists, could pass for an underfunded YMCA. The world’s highest-performing schools—those in Finland and South Korea, which perennially score at the top of the Program for International Student Assessment rankings—feature austere classrooms that look as if they haven’t changed since the 1950s. The point of this tip is not moral; it’s neural. Simple, humble spaces help focus attention on the deep-practice task at hand: reaching

View project_euler_84_monopoly.rb
# (solved at the airport)
do_print = false
num_trials = 1000
first, *mids, last =
"GO A1 CC1 A2 T1 R1 B1 CH1 B2 B3 JAIL
H2 C1
T2 U1
H1 C2
View zlib_example.rb
s = 'require"io/console";puts"\e[?25l\e[?1000h";at_exit{puts"\e[?25h\e[?1000l"};x=y=ax=ay=0;dy=dx=0.5;h,w,e,fg=*$>.winsize,!7,7;def r(n,v,i=(3*Math.tanh(v)).to_i+2)"\e[38;5;16m#{[196,208,184,38,21].map{|n|"\e[48;5;#{n}m"}.map.with_index{|c,j|c+(j==i&&n||?-)}*""}\e[40;37m %6.2f"%v;end;{$stdin.raw{case$stdin.readpartial(20);when"\e[A";ay-=0.1;when"\e[B";ay+=0.1;when"\e[C";ax+=0.1;when"\e[D";ax-=0.1;when"\3";e=!e;when/\e\[M (.)(.)/;dx,dy,x,y=0,0,*[$1,$2].map{|n|n.force_encoding(Encoding::ASCII_8BIT).ord-32};when/^\d/;fg=$&;end until e}};(sleep 0.05;dx+=ax*0.1;dy+=ay*0.1;x+=dx/2;y+=dy/2;y,dy=y<1?[1,-dy*0.8]:y>h ?[h,-dy*0.8]:[y,dy];x,dx=x<1?[1,-dx*0.8]:x+1>w ?[w-2,-dx*0.8]:[x,dx];$><<"\e[40m\e[H\e[2J#{r ?x,dx} x-velocity\r\n#{r ?y,dy} y-velocity\r\n#{r ?x,ax} x-accel\r\n#{r ?y,ay} y-accel\e[#{y.to_i};#{x.to_i}H\e[4#{fg}m ")until e'
# ===== Default =====
require 'zlib'
Zlib::Deflate.deflate(s).length # => 546
s.length # => 843
# ===== Playing with options =====
compression =
View huffman_encoding.rb
# Huffman encoding. Just for fun, I didn't verify that it is actually correct
# Glanced at the trees in this document to get the idea, then just rolled with what made sense
class Huffman
def self.for_string(string)
freqs = string.chars.group_by(&:itself).map { |c, cs| cs.length, c }
freqs.unshift, freqs.shift)
View terminal_physics_engine.rb
require 'io/console'
puts"\e[?25l\e[?1000h";at_exit{puts"\e[?25h\e[?1000l"} # no cursor, yes mouse
x = y = dy = ax = ay = 0; dy = dx = 0.5
height, width = $>.winsize
def hot_cold(name, val)
index = (3*Math.tanh(val)).to_i+2
"\e[38;5;16m" << [{r:5},{r:5,g:2},{r:4,g:4},{g:3,b:4},{b:5}]
.map{ |r:0,g:0,b:0| "\e[48;5;#{r*36+g*6+b+16}m" }
.map.with_index{|c,i| c+(i==index ? name : "-") }
.join <<
Something went wrong with that request. Please try again.