Skip to content

Instantly share code, notes, and snippets.

View longlostnick's full-sized avatar

Nick Larson longlostnick

View GitHub Profile
@longlostnick
longlostnick / find_replace_all.rb
Created December 18, 2012 21:15
Find and replace using split and join.
string = "this is cool"
"*#{string}*".split("cool").join("dumb")[1..-2]
# => "this is dumb"
@longlostnick
longlostnick / each_async.js
Created March 9, 2012 01:18
Asynchronous Each
function eachAsync(arr, _block, _callback) {
var i = 0, processing = false;
var thread = setInterval(function() {
if (!processing) {
processing = true;
_block(arr[i]);
i++;
if (i >= arr.length) {
clearInterval(thread);
if (_callback) _callback();
blockquote {
background: #f7f7f7;
font-family: Georgia;
padding: 14px 14px 12px 12px !important;
border: 1px solid #e7e7e7;
margin: 1em 3em;
text-align: right;
font-size: 10pt;
-webkit-border-radius: 5px;
@longlostnick
longlostnick / gist:1712614
Created January 31, 2012 20:08
Cross browser iframe scrollWidth/scrollHeight and resize iframe to content
// Works for me in IE7+, Webkit, and FF
function resizeToContent(frame_id) {
var my_frame = document.getElementById(frame_id);
var content_width = my_frame.contentWindow.document.documentElement.scrollWidth;
var content_height = my_frame.contentWindow.document.documentElement.scrollHeight;
my_frame.style.width = content_width + 'px';
my_frame.style.height = content_height + 'px';
}
@longlostnick
longlostnick / date_time_validator.rb
Created January 18, 2012 03:50
Validates DateTime values in a Rails app
# this sucker takes care of validating datetime fields before rails gets there and
# messes everything up. it should preserve the local time zone from the user input,
# and check for nil. takes date strings of the format m/d/yyyy m:h (am/pm)
# this goes in the model
validates_with DateTimeValidator, :fields => [:add_date]
# this goes in some place like lib/date_time_validator.rb
class DateTimeValidator < ActiveModel::Validator
DATETIME_FORMAT = "%m/%d/%Y %I:%M %P"
@longlostnick
longlostnick / trim.js
Created November 28, 2011 19:44
trim whitespace
var trimmed = str.replace(/^\s+|\s+$/g, '');
@longlostnick
longlostnick / _extend.js
Created October 26, 2011 23:19
for prettier javascript
function _extend(obj) {
var sources = Array.prototype.slice.call(arguments, 1);
for (var i=0; i<sources.length; i++) {
var source = sources[i];
for (var prop in source) {
obj[prop] = source[prop];
}
}
}
@longlostnick
longlostnick / gist:1262136
Created October 4, 2011 16:44
Ruby Array#bsearch
class Array
def bsearch(e, l = 0, u = length - 1)
return if l>u;m=(l+u)/2;e<self[m]?u=m-1:l=m+1;e==self[m]?m:bsearch(e,l,u)
end
end
@longlostnick
longlostnick / LICENSE.txt
Created September 29, 2011 22:38 — forked from p01/LICENSE.txt
Sudoku Solver in 140bytes
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Mathieu 'p01' Henri <http://www.p01.org/releases/>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@longlostnick
longlostnick / gist:1251339
Created September 29, 2011 17:24
Delete all empty/false elements from hash recursively
# (v.respond_to?(:empty?) ? v.empty? : !v) is basically rails' .blank? in plain ruby
class Hash
def delete_blank
delete_if do |k, v|
(v.respond_to?(:empty?) ? v.empty? : !v) or v.instance_of?(Hash) && v.delete_blank.empty?
end
end
end