Skip to content

Instantly share code, notes, and snippets.

@sinisterchipmunk
sinisterchipmunk / line
Created April 18, 2011 09:57
Very simple script to display a specified line in a file, plus the line immediately preceding and following it. I whipped this up as a quick solution to manually following a particularly vague stack trace from Jax (github.com/sinisterchipmunk/jax).
#!/usr/bin/env ruby
def fail(msg)
puts msg
puts
exit
end
fail "Usage: line [filename] [lineno]" if !ARGV[0] || !ARGV[1] || !File.file?(ARGV[0])
@sinisterchipmunk
sinisterchipmunk / tree
Created April 18, 2011 10:05
I found myself having to construct a series of near-identical directory structures, and wanted a quick visualization of their contents. Was too lazy to open a file manager, and was happy for the excuse to code some Ruby. This was the result.
#!/usr/bin/env ruby
TAB_STOP = 2
$directories_only = false
$use_color = true
class Tree
attr_reader :word, :branches
@sinisterchipmunk
sinisterchipmunk / autotest.rb
Created May 23, 2011 15:04
Quick-and-dirty autotest without installing Autotest
#!/usr/bin/env ruby
class Autotester
MONITOR_MATCH_PATTERN = "**/*.rb"
TEST_COMMAND = "rspec spec"
def cache
@cache ||= {}
end
set :deploy_to, "public_html" # the remote path where the gem index lives
set :build_command, "bundle exec rake build" # the command to build the gem locally
set :user, "rubygems" # the user to log in to the server with
# the rubygems server to deploy to
server "gems.example.com", :app
desc "Deploy the gem and rebuild the index"
task :deploy do
build
@sinisterchipmunk
sinisterchipmunk / assets.rake
Created September 13, 2011 13:53 — forked from shedd/assets.rake
Check asset encoding for valid UTF-8; if not valid, show compatible encodings
namespace :assets do
task :check => :environment do
paths = ["app/assets", "lib/assets", "vendor/assets"]
paths.each do |path|
dir_path = Rails.root + path
if File.exists?(dir_path)
dir_files = File.join(dir_path, "**")
@sinisterchipmunk
sinisterchipmunk / dungeon.js.coffee
Created November 27, 2011 00:17
example of dungeon model as of section 8.5 of the Jax guides: http://guides.jaxgl.com/getting_started.html#drawing-the-scene
parse = (str) ->
vec = str.split /,\s*/
return [ parseFloat(vec[0]), 0.0, parseFloat(vec[1]) ]
Jax.getGlobal()['Dungeon'] = Jax.Model.create
addTorches: (world) ->
if @map
for z in [0...@map.length]
row = @map[z]
for x in [0...row.length]
@sinisterchipmunk
sinisterchipmunk / dice.rb
Created January 16, 2012 01:33
Code examples for "Welcome to my world!" - http://localhost:3000/mist/posts/welcome-to-my-world
class Dice
def initialize(count, sides)
@count, @sides = count, sides
end
def roll
@count.times.inject(0) { |a,i| a + rand(sides) }
end
end
@sinisterchipmunk
sinisterchipmunk / Example 1
Created January 27, 2012 15:17
Code examples for "Understanding REST in Rails 3" - http://thoughtsincomputation.com/understanding-rest-in-rails-3
REST request | CRUD request | Action
----------------|----------------------|-------
POST /posts | /posts/create | Create
GET /posts/1 | /posts/show/colin | Read
PUT /posts/1 | /posts/update/colin | Update
DELETE /posts/1 | /posts/destroy/colin | Delete
Jax.getGlobal().ApplicationHelper = Jax.Helper.create
patch_world: ->
Jax.World.prototype.pick_all_visible = () ->
context = this.context
w = context.canvas.width
h = context.canvas.height
data = new Uint8Array(w*h*4)
data.w = w
data.h = h
data.f = f = 4
$("canvas").remove();
c = document.createElement('canvas');
c.width = c.height = 300;
$(document.body).append(c);
$(c).css("width", "300px");
$(c).css("height", "300px");
$(c).css("position", "absolute");
$(c).css("top", "0px");
$(c).css("left", "0px");
$(c).css("border", "1px solid #000");