Skip to content

Instantly share code, notes, and snippets.

View EvilScott's full-sized avatar

Molly Reis EvilScott

View GitHub Profile
@EvilScott
EvilScott / diagonals.rb
Created February 6, 2012 23:04
Retrieve diagonals from array of arrays in Ruby
class Array
def diagonals
[self, self.map(&:reverse)].inject([]) do |all_diags, matrix|
((-matrix.count + 1)..matrix.first.count).each do |offet_index|
diagonal = []
(matrix.count).times do |row_index|
col_index = offet_index + row_index
diagonal << matrix[row_index][col_index] if col_index >= 0
end
all_diags << diagonal.compact if diagonal.compact.count > 1
@EvilScott
EvilScott / apprunner.rb
Created February 13, 2012 22:44
Use Sinatra to open a browser window (and close Sinatra when the window closes)
%w[sinatra watir-webdriver].each { |gem| require gem }
class MyApp < Sinatra::Base
get('/') { "hello world" }
end
class AppRunner
def initialize
@EvilScott
EvilScott / jimson-sinatra.rb
Created April 11, 2012 15:33
Jimson and Sinatra playing together
require 'sinatra'
require 'jimson'
class Api
extend Jimson::Handler
def get_data
{'foo' => 'bar'}
end
end
@EvilScott
EvilScott / image_resizer.rb
Created April 20, 2012 19:54
Image processing server using Sinatra and MiniMagick
require 'mini_magick'
class ImageResizer
attr_accessor :height, :width, :padding, :stretch, :grayscale
def initialize(path)
@image = MiniMagick::Image.open(path)
end
@EvilScott
EvilScott / session_fun.rb
Created April 25, 2012 20:28
Guessing game fun using sessions with Sinatra
require 'sinatra'
require 'haml'
require 'json'
enable :sessions
get '/' do
session[:answer] = %w[A B C D].sample
haml :index
end
@EvilScott
EvilScott / longpoll.rb
Created November 27, 2012 17:23
example of long polling using sinatra and jquery
require 'json'
require 'haml'
require 'sinatra'
get '/' do
haml :view
end
get '/request' do
content_type :json
@EvilScott
EvilScott / .bash_profile
Last active December 20, 2015 16:18
git prompt
source ~/git-prompt.sh
source ~/git-completion.bash
PS1='\u:\w\[\033[32m\]$(__git_ps1)\[\033[0m\]$ '
@EvilScott
EvilScott / .gitconfig
Created January 10, 2014 16:29
Git Config
[user]
email = <YOUR EMAIL>
name = <YOUR NAME>
[color]
diff = auto
status = auto
branch = auto
interactive = auto
ui = true
@EvilScott
EvilScott / my_parser.rb
Created April 22, 2014 07:37
An example grammar/parser using the Ruby gem Treetop
require 'treetop'
module Mine
class IntegerLiteral < Treetop::Runtime::SyntaxNode
def to_i
text_value.to_i
end
end
@EvilScott
EvilScott / start.sh
Created November 14, 2014 19:31
Express + Forever start script
#!/bin/sh
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
"$DIR/node_modules/.bin/forever" start \
--watch --watchDirectory ${DIR} \
--watchIgnore "$DIR/.*/*" \
--watchIgnore "$DIR/public/*" \
--watchIgnore "$DIR/logs/*" \
--append -l "$DIR/logs/app.log" \
--minUptime 1000 \
--spinSleepTime 1000 \