Skip to content

Instantly share code, notes, and snippets.

View faith.rb
cls = "single#{i.zero? ? " first" : i == images.size - 1 ? " last" : ""}"
cls = "single" + {0 => " first", images.size - 1 => " last"}[i]
View drugs_and_convenience.rb
class Integer
def method_missing meth, *args
super unless meth[/base\d+/]
to_s meth[4,2.0].to_i
end
end
@andkerosine
andkerosine / ws-asm.rb
Created Sep 9, 2012
Whitespace assembler in Ruby
View ws-asm.rb
ops = %w[push pop dup copy swap slide add sub mul div mod store load label call jump jz jn ret exit ichr inum ochr onum]
quat = [5, 31, 29, 25, 30, 27, 149, 150, 151, 153, 154, 41, 42, 53, 54, 55, 57, 58, 59, 63, 185, 186, 181, 182]
ops = Hash[ops.zip quat.map { |q| q.to_s(4).tr '123', " \t\n" }]
def encode num
(num < 0 ? "\t" : ' ') + ('%b' % num.abs).tr('01', " \t") + "\n"
end
source = File.read ARGV[0]
source.gsub! /\s*[;#].+/, ''
View dp97i.asm
push 3
push 0
dup
inum
load
0:
swap
push 3
mul
View dp96e.asm
push 0
dup
inum
load
dup
push 20
div
0:
dup
@andkerosine
andkerosine / fen.rb
Created Aug 24, 2012
FEN Move Validator
View fen.rb
def side piece
piece == ' ' ? -1 : piece =~ /[a-z]/
end
fen = File.read ARGV.shift
board = [*'1'..'8'].reverse.product [*'a'..'h']
from, to = *ARGV.map { |a| board.index a.reverse.split // }
raise 'Wat.' if from == to
board, act, _ = fen.split
@andkerosine
andkerosine / raskell.rb
Created Aug 15, 2012
Haskell-like list comprehensions in Ruby
View raskell.rb
$stack, $draws = [], {}
def method_missing *args
return if args[0][/^to_/]
$stack << args.map { |a| a or $stack.pop }
$draws[$stack.pop(2)[0][0]] = args[1] if args[0] == :<
end
class Array
def +@
View fun.rb
class Symbol
def | other
-> arg { arg.send(self).send(other) }
end
end
class Proc
def | other
-> arg { call(arg).send(other) }
end