Skip to content

Instantly share code, notes, and snippets.

View kyledecot's full-sized avatar
🤘

Kyle Decot kyledecot

🤘
View GitHub Profile
@kyledecot
kyledecot / gist:2159149
Created March 22, 2012 15:52
Unobtrusive JS for submitting a delete link
jQuery('body').on('click', '[data-method="delete"]', function(event) {
event.preventDefault();
var confirm_msg = jQuery(this).data('confirm');
var form = jQuery('<form />').attr({
method : 'POST',
action : jQuery(this).attr('href')
});
@kyledecot
kyledecot / each_slice.js
Created May 11, 2012 17:57
jQuery.each_slice inspired by Ruby's each_slice
(function($) {
$.each_slice = function(num, things, iterator) {
var tmp = [];
$.each(things, function(index, thing) {
tmp.push(thing);
if (tmp.length >= num || (index + 1) >= things.length) {
iterator(tmp);
tmp = [];
}
});
@kyledecot
kyledecot / string_additions.rb
Created May 21, 2012 19:12
Ruby String#to_md5
require 'digest/md5'
class String
def to_md5
Digest::MD5.hexdigest(self)
end
end
# "Hello World!".to_md5
@kyledecot
kyledecot / gist:2782141
Created May 24, 2012 15:11
Alias for opening current directory in Coda 2
alias coda='open -a /Applications/Coda\ 2.app .'
@kyledecot
kyledecot / laravel-ujs.js
Created June 19, 2012 14:40
laravel UJS
(function($, undefined) {
// Shorthand to make it a little easier to call public laravel functions from within laravel.js
var laravel;
$.laravel = laravel = {
// Link elements bound by jquery-ujs
linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with]',
// Select elements bound by jquery-ujs
inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
@kyledecot
kyledecot / gist:2954926
Created June 19, 2012 15:49
jQuery.without
$.without = function(first, second) {
return $.grep(first, function(n, i){
return $.inArray(n, second) == -1;
});
};
// Usage...
console.log($.without([1,2,3,4,5], [4,5]));​​ // [1,2,3]
@kyledecot
kyledecot / gist:5512014
Created May 3, 2013 17:57
CLow Dice Roller in Ruby
([*1..3].map{[*1..6].sample}).sort.join(', ')
Resource::POSSIBLE_CLASSES.each do |klass|
define_method klass.name.tableize do
resource_ids = group_memberships.pluck(:resource_id)
klass.where(klass.arel_table[:id].in(resource_ids))
end
define_method "has_#{klass.name.underscore}?" do |object|
object.resource_id.in? send(klass.name.tableize.to_sym).pluck(:id)
end
end
# Given a CSV of people, display them grouped together by their favorite colors
require 'csv'
# Gather the data...
results = Array.new.tap do |a|
CSV.foreach File.expand_path('../people.csv', __FILE__), headers: true do |row|
a << row.to_hash
end
import java.util.Random;
import java.util.Scanner;
public class Project {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
int number_of_questions = 4;