Skip to content

Instantly share code, notes, and snippets.

@codeincontext
codeincontext / gist:2299966
Created April 4, 2012 09:33
Show logger.debug lines in green in the Rails development log
config.after_initialize do
class<<Rails.logger
def debug_with_color(message)
message_color = "0;32;1"
colored_message = "\e[#{message_color}m#{message}\e[0m"
debug_without_color(colored_message)
end
alias_method_chain :debug, :color
end
@codeincontext
codeincontext / iPlayer-filter.user.js
Created April 9, 2012 11:07
UserScript to hide certain programs from the iPlayer web interface
// ==UserScript==
// @name iPlayer Filter
// @namespace http://skatty.me/
// @description Hides crap shows from BBC iPlayer
// @include http://bbc.co.uk/iplayer/*
// @include http://www.bbc.co.uk/iplayer/*
// ==/UserScript==
var load,execute,loadAndExecute;load=function(a,b,c){var d;d=document.createElement("script"),d.setAttribute("src",a),b!=null&&d.addEventListener("load",b),c!=null&&d.addEventListener("error",c),document.body.appendChild(d);return d},execute=function(a){var b,c;typeof a=="function"?b="("+a+")();":b=a,c=document.createElement("script"),c.textContent=b,document.body.appendChild(c);return c},loadAndExecute=function(a,b){return load(a,function(){return execute(b)})};
@codeincontext
codeincontext / gist:2343241
Created April 9, 2012 12:58
Print the headers from a markdown document into 1.2.3 format
file = '/Users/skattyadz/Dropbox/PlainText/FYP.txt'
toc = ''
number_stack = [0]
File.readlines(file).each do |line|
if line[0] == '#'
hash_count = line.count('#')
if hash_count > number_stack.length
# down a level
@codeincontext
codeincontext / gist:2400950
Created April 16, 2012 19:31
Eager load a certain number of levels of a nested resource
def self.nested_include(nested_attribute, additional_attributes=[], nesting_level=6)
attributes = additional_attributes
nesting_level.times do
attributes = additional_attributes + [{nested_attribute => attributes}]
end
includes(attributes)
end
scope :with_nesting, nested_include(:children_ordered, [:user, :project])
Compiled jquery.tmpl.js (0ms) (pid 22675)
Compiled jquery.ui.datepicker-lb.js (1ms) (pid 22675)
Compiled jquery.ui.datepicker.js (1ms) (pid 22675)
Compiled jquery.ui.core.js (1ms) (pid 22675)
Compiled jquery.js (1ms) (pid 22675)
Compiled jquery.ui.datepicker-lv.js (1ms) (pid 22675)
Compiled jquery.ui.datepicker-vi.js (1ms) (pid 22675)
Compiled jquery.ui.datepicker-cy-GB.js (1ms) (pid 22675)
Compiled jquery.effects.clip.js (1ms) (pid 22675)
Compiled jquery.effects.core.js (0ms) (pid 22675)
\documentclass[a4paper,abstract=on,12pt]{scrartcl}
\usepackage{amssymb,amsmath}
\usepackage[a4paper]{geometry}
\geometry{margin=1in}
\usepackage{setspace}
\onehalfspacing
% These commands keep the koma system from making sans serif section headings
require 'timeout'
files_to_watch = %w{FYP.txt template.tex FYP.bib}
guard :shell do
files_regex = Regexp.new(files_to_watch.join("|"))
watch(files_regex) { regenerate }
end
def regenerate

The researcher believes that well written code should be self-documenting. Ruby code can be so similar to pseudo-code that descriptive commenting would only paraphrase the contents of the line, and could in fact make code less clear.

Below are some code samples from the project to demonstrate the self-descriptive nature of code.

validates_presence_of :password, :on => :create

This line tells the current model that it should validate the presence of a password when it is created.

This is a snippet from sessions_controller.rb, first uncommented, and then with 'descriptive' comments:

Handlebars.Utils.isEmpty = function(value) {
if (typeof value === "undefined") {
return true;
} else if (value === null) {
return true;
} else if (value === false) {
return true;
} else if (value === '') {
return true;
} else if (value === 'null') {
@codeincontext
codeincontext / gist:2772254
Created May 22, 2012 23:14
Toying with this pattern for complex JS objects (replicating 'class' and 'instance' methods/variables)
// Constants
Solar = {
START_HEALTH: 100,
RADIUS: 10,
COLOR: "FF0",
RECYCLE: 50,
RANGE: 50,
ENERGY_PRODUCED: 0.5,
STORAGE: 25,
COST: 200,