Skip to content

Instantly share code, notes, and snippets.

@eric1234
eric1234 / p11.rb
Created February 10, 2010 04:53
Problem 11 on Project Euler
data = DATA.read.split("\n").collect {|row| row.split(' ').collect(&:to_i)}
possible = []
width = data.size
for i in (0...width)
for j in (0...width)
possible << (0...4).inject(1) {|t, k| t * data[i][j+k]} unless j+3 >= width
possible << (0...4).inject(1) {|t, k| t * data[i+k][j]} unless i+3 >= width
possible << (0...4).inject(1) {|t, k| t * data[i+k][j+k]} unless i+3 >= width || j+3 >= width
possible << (0...4).inject(1) {|t, k| t * data[i+k][j-k]} unless i+3 >= width || j-3 < 0
@eric1234
eric1234 / bounce.rb
Created April 19, 2010 13:15
Heroku restart app
require 'rubygems'
require 'heroku'
require 'sinatra'
get '/' do
# Replace XXXX@YYYYY.com with account email and ZZZZZZ with password.
# Replace APP_NAME with the app you want to restart
Heroku::Client.new('XXXX@YYYYY.com', 'ZZZZZZ').restart 'APP_NAME'
'Successfully restarted'
end
@eric1234
eric1234 / shadow-move.js
Created May 18, 2010 17:20
Shadowbox extension: Will move inline content instead of copy
/**
* A shadowbox player that replaces the inline (html) player with one
* that will move the content instead of copying it. This preserves
* any callbacks registered on the content, fields populated by the
* user, and ensures element id's are unique.
*
* To use just include after including shadowbox and the implementation
* of the HTML player will be adjusted.
*
* Note this player is dependent of Prototype but could probably easily
@eric1234
eric1234 / random_string.js
Created June 15, 2010 20:09
Random string generator
Array.prototype.random = function() {
return this[parseInt(Math.random() * this.length)];
}
String.random = function(len) {
var chars = $R('a', 'z').toArray().concat($R('A', 'Z').toArray());
return $R(1, len).inject('', function(m, i) {return m + chars.random()});
}
@eric1234
eric1234 / config.js
Created June 18, 2010 13:37
Basecamp theming for CKEditor
CKEDITOR.lang.load('en', 'en', function() {
CKEDITOR.lang['en']['numberedlist'] = 'Numbers';
CKEDITOR.lang['en']['bulletedlist'] = 'Bullets';
CKEDITOR.replace('project_description', {
toolbar: [['Bold', 'Italic', '-', 'NumberedList', 'BulletedList']],
toolbarCanCollapse: false,
resize_enabled: false,
customConfig: '',
removePlugins: 'elementspath'
});
@eric1234
eric1234 / banner.js
Created July 23, 2010 13:54
Banner rotation script
/* Manages the initialization rotation of banner images */
var Banner = Class.create({
/*
* The first image needs to be a <img> element that already exists
* on the page. We rely on the browser to load that image as the page
* is loading. All other images are then passed in as a URL which
* is then managed by this script. For example:
*
* new Banner('first-image', '/images/banner-2.png', '/images/banner-3.png');
*
@eric1234
eric1234 / hover.coffee
Created July 23, 2010 14:09
Non-obtrusively and easily swap images on mouse hover
# Handles image swapping on the given element
class @HoverImage
constructor: (img) ->
# If a string is given then find the element by that id in the document
# If element given then just use element. Also assume the element (item
# that monitors events) is the same as the image (thing we are swapping).
@element = @img = if img instanceof String
HoverImage.css('#'+img)[0]
@eric1234
eric1234 / image-defer.js
Last active July 10, 2017 15:17
Deferred image loading
var ImageDefer = Class.create({
initialize: function(placeholder) {
this.placeholder = $(placeholder);
this.placeholder.update('Loading image...');
if(ImageDefer.page_loaded) {
this.preload();
} else {
Event.observe(window, 'load', (function() {this.preload()}).bind(this));
}
@eric1234
eric1234 / README.md
Last active December 12, 2015 01:11
Database-backed config object

DB-backed Hash for Ruby

Useful to store random bits of config that don't fit neatly elsewhere. Installation (using gist-dep):

gist-dep add -p app/models 519630/db_config.rb
gist-dep add -p db/migrate/20151211193900_add_config.rb 519630/migration.rb
gist-dep add -p test/models 519630/db_config_test.rb

Sure there is true key/value stores like redis that are better in most ways. But sometimes you have just a bit of key/value that you need, you are already using ActiveRecord and don't want to complexity of yet another DB. Usage:

@eric1234
eric1234 / marquee.coffee
Last active October 5, 2016 10:48
<marquee> element lives! - Emulating the behavior of the old marquee element with CoffeeScript/JavaScript
# https://gist.github.com/eric1234/548690
class @Marquee
constructor: (@element, @options={}) ->
@clean @element
@stopped = true
# Set defaults
@options['duration'] or= 30