View fizzbuzz.rb
a=b=c=(1..100).each do |num| | |
print num, ?\r, | |
("Fizz" unless (a = !a) .. (a = !a)), | |
("Buzz" unless (b = !b) ... !((c = !c) .. (c = !c))), | |
?\n | |
end |
View .vimrc
"" | |
"" Thanks: | |
"" Gary Bernhardt <destroyallsoftware.com> | |
"" Drew Neil <vimcasts.org> | |
"" Tim Pope <tbaggery.com> | |
"" Janus <github.com/carlhuda/janus> | |
"" | |
set nocompatible |
View cocoa_keypress_monitor.py
# cocoa_keypress_monitor.py by Bjarte Johansen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. | |
from AppKit import NSApplication, NSApp | |
from Foundation import NSObject, NSLog | |
from Cocoa import NSEvent, NSKeyDownMask | |
from PyObjCTools import AppHelper | |
class AppDelegate(NSObject): | |
def applicationDidFinishLaunching_(self, notification): | |
mask = NSKeyDownMask |
View rails_routing_invalid_chars_fix.rb
# Fix for a Rails - Ruby 1.9 bug | |
# Rails Router, now that it's UTF-8 default, blows up when routing requests | |
# with invalid chars in the URL; it should properly return a 400 error | |
# Have to monkey-patch the fix in, since it's not scheduled for release until | |
# Rails 4.0. | |
# Adapted Andrew White (pixeltrix)'s fix at | |
# https://github.com/rails/rails/commit/3fc561a1f71edf1c2bae695cafa03909d24a5ca3, | |
# but edited to work in 3.0.x. | |
# 3.1.x, 3.2.x compatibility unknown | |
require 'action_dispatch/routing/route_set' |
View gist:2762570
// keep models fetched in different views in sync | |
// Finding and creating models | |
// =========================== | |
// | |
// We might need to get a reference to a model from anywhere in our code | |
// and we want to make sure that these references all point to the same model. | |
// Otherwise updates to one copy of the model won't affect another. | |
// | |
// The methods here let you get models through a wrapper that will either | |
// create the model if it doesn't exist anywhere, or return a reference to |
View gist:2645620
class UserRegistrationService | |
def initialize(params) | |
@params = params | |
end | |
def register | |
user = User.new(@params) | |
if user.save | |
# send_email |
View uri.js
var parser = document.createElement('a'); | |
parser.href = "http://example.com:3000/pathname/?search=test#hash"; | |
parser.protocol; // => "http:" | |
parser.host; // => "example.com" | |
parser.port; // => "3000" | |
parser.pathname; // => "/pathname/" | |
parser.search; // => "?search=test" | |
parser.hash; // => "#hash" |
View gist:2246085
def compile_asset?(path) | |
# ignores any filename that begins with '_' (e.g. sass partials) | |
# all other css/js/sass/image files are processed | |
if File.basename(path) =~ /^[^_].*\.\w+$/ | |
puts "Compiling: #{path}" | |
true | |
else | |
puts "Ignoring: #{path}" | |
false | |
end |
View admin.rb
# Let's assume these models Department, Subject, User, Allotment, Test | |
# Allotment table has department_id, subject_id, user_id | |
# Test table has department_id, :subject_id | |
# Problem: allow the user to edit a test if he is alloted the subject | |
# for that department OR if he is special (dean) OR if the number of allotments | |
# of that user in that department exceeds 3 | |
# | |
class Ability |
View gist:1559754
# Install with: | |
# bash < <(curl -L https://raw.github.com/gist/1559754) | |
# | |
# Reference: http://blog.wyeworks.com/2011/11/1/ruby-1-9-3-and-ruby-debug | |
echo "Installing ruby-debug with ruby-1.9.3-p125 ..." | |
curl -OL http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem | |
curl -OL http://rubyforge.org/frs/download.php/75415/ruby-debug-base19-0.11.26.gem |