Skip to content

Instantly share code, notes, and snippets.

View gist:1262136
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
@ifightcrime
ifightcrime / _extend.js
Created Oct 26, 2011
for prettier javascript
View _extend.js
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];
}
}
}
@ifightcrime
ifightcrime / trim.js
Created Nov 28, 2011
trim whitespace
View trim.js
var trimmed = str.replace(/^\s+|\s+$/g, '');
@ifightcrime
ifightcrime / date_time_validator.rb
Created Jan 18, 2012
Validates DateTime values in a Rails app
View date_time_validator.rb
# 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"
View each_async.js
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();
@ifightcrime
ifightcrime / accept_nested_attributes.js
Last active Nov 3, 2015
FormBuilder addition for models that accept nested attributes.
View accept_nested_attributes.js
function addNestedRow(e) {
e.preventDefault();
// http://railscasts.com/episodes/197-nested-model-form-part-2
var html = $('#my-attribute-link-template').html();
var $attr_row = $(html.replace(/new_MyAttribute/g, new Date().getTime()));
$('#my-attributes').append($attr_row);
};
@ifightcrime
ifightcrime / find_replace_all.rb
Created Dec 18, 2012
Find and replace using split and join.
View find_replace_all.rb
string = "this is cool"
"*#{string}*".split("cool").join("dumb")[1..-2]
# => "this is dumb"
@ifightcrime
ifightcrime / install_vbox_guest_additions.md
Last active Dec 11, 2015
Install VirtualBox Guest Additions (Linux)
View install_vbox_guest_additions.md

Devices > Install Guest Additions CD Image

mount /dev/sr0 /media/cdrom && cd /media/cdrom
./VBoxLinuxAdditions.run
View print_repeating.rb
def print_repeating(array)
0.upto(array.size - 1) do |i|
flag = array[array[i].abs]
flag = 1 if flag.nil?
if flag >= 0
array[array[i].abs] = -flag
else
@ifightcrime
ifightcrime / gist:5461340
Created Apr 25, 2013
Try to detect when someone's mouse is moving upward off the page
View gist:5461340
(function() {
var current_scroll = 0;
var last_mouse_y = null;
$(document)
.scroll(function() {
current_scroll = $(this).scrollTop();
})
.mousemove(function(e) {
You can’t perform that action at this time.