Skip to content

Instantly share code, notes, and snippets.

View AMHOL's full-sized avatar

Andy Holland AMHOL

  • Manchester, UK
  • 23:02 (UTC +01:00)
View GitHub Profile
@AMHOL
AMHOL / tricks.md
Last active December 17, 2015 04:49
Cool Ruby snippets

USEFUL TRICKS

Array#reduce

Array.reduce will inject and initial value into a block and pass in each value in turn until all values in the array has been iterated i.e

([1] * 10).reduce(0) { |total, current_value| total + current_value } #= 10
# Inject does the same
([1] * 10).inject(0) { |total, current_value| total + current_value } #= 10

However, if we simply require a sum of all values in the array, we can take advantage of Symbol#to_proc as follows:

@AMHOL
AMHOL / gist:5555467
Last active December 17, 2015 05:08
Windows phone console
if ( (typeof window.console == 'undefined' || typeof window.console.log != 'function') && !!window.location.href.match(/\?console=true/i) ) {
var ul = _createTag('ul', false, {
'line-height': '30px',
'background-color': '#fff',
'list-style-type': 'square',
color: '#000',
position: 'absolute',
top: 0,
right: 0,
left: 0,
@AMHOL
AMHOL / jquery.swipe.js
Created May 22, 2013 15:37
jQuery swipe library with support for android/iPad touch events and mouse events
(function($) {
'use strict';
// settings
$.fn.swipe = function(options) {
var args = Array.prototype.slice.call(arguments);
// bind using bind or on?
var bind = typeof $.fn.on == 'function' ? 'on' : 'bind';
var unbind = typeof $.fn.off == 'function' ? 'off' : 'unbind';
// events to bind
var events = {
@AMHOL
AMHOL / gist:5730159
Created June 7, 2013 15:34
jQuery rails-like methods for string manip
$.truncate = function(text, length) {
if ( length === undefined ) length = 255;
if ( text.length <= length ) return text;
return text.substring(0, length) + '...';
};
$.number_with_delimiter = function(number, delimiter) {
var part, new_number = [];
if ( delimiter === undefined ) delimiter = ',';
number = number.toString().split('').reverse();
@AMHOL
AMHOL / document.createStyleSheet.js
Last active December 20, 2015 01:59
Normalize IE's createStyleSheet
if ( typeof document.createStyleSheet !== 'function' ) {
document.createStyleSheet = function(url, index) {
var styleElement;
if ( typeof url === 'string' ) {
styleElement = document.createElement('style');
if ( url.replace('{', '') !== url ) {
if ( styleElement.styleSheet ) {
styleElement.styleSheet.cssText = url;
} else {
styleElement.appendChild(document.createTextNode(url));
@AMHOL
AMHOL / acts_as_taggable_on.rb
Created August 1, 2013 14:41
Initializer for making rails acts-as-taggable-on v2.1.1 case-insensitive (config/initializers/acts_as_taggable_on.rb)
ActsAsTaggableOn::TagList.delimiter = '|'
# https://github.com/mbleigh/acts-as-taggable-on/blob/v2.1.1/lib/acts_as_taggable_on/utils.rb
# This is a hack to remove case-insensitivity on the acts-as-taggable (v2.1.1)
# RE: Ticket - http://redmine.cirrus-connect.com/issues/249
module ActsAsTaggableOn::Utils::OverallMethods
private
def like_operator
using_postgresql? ? 'LIKE' : 'LIKE BINARY'
end
@AMHOL
AMHOL / bashrc.bat
Created August 9, 2013 09:43
Windows bashrc
:: This is my windows bashrc.bat file, I only really use it for doskey commands,
:: To get this to work go to Start > Type "regedit" > "HKEY_LOCAL_MACHINE" > "SOFTWARE" > "Microsoft" > "Command Processor"
:: look for "AutoRun" and change the "Data" field to an absolute path to THIS file. If it doesn't exist right-click and go to
:: "New" > "String Value" > type "AutoRun" and change the "Data" field to an absolute path to THIS file.
:: This file makes "rake" use "bundle exec rake", "s" starts my rails server, "c" for rails console, "m" for database
:: migrations and "mysql" is changed to "mysql -u root", this allows me to import databases easily with
:: "mysql dbname < file.sql"
@echo off
@AMHOL
AMHOL / sti_polymorphic.rb
Created October 15, 2013 09:04
Rails STI with Polymorphic relationship fix
def self.inherited(subclass)
super(subclass)
subclass.send(:class_eval, <<-eoruby, __FILE__, __LINE__ + 1)
def self.base_class
self
end
eoruby
end
@AMHOL
AMHOL / gist:7212979
Created October 29, 2013 11:29
Rails autoloading - Read later
http://urbanautomaton.com/blog/2013/08/27/rails-autoloading-hell/
(function(window, $, undefined) {
'use strict';
$.fn.textWidth = function() {
var $span = $('<span />', {
text: $(this).text() || $(this).val()
}).css({
margin: 0,
padding: 0,
width: 'auto',
font: $(this).css('font'),