Skip to content

Instantly share code, notes, and snippets.

Chris Zetter zetter

Block or report user

Report or block zetter

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View gist:295978
class Hash
def hash_map()
result = self.inject({}) do |hash, (k, v)|
hash.merge(yield(k, v))
end
end
end
{:a => 1, :b => 2}.hash_map {|k,v| {k.to_s*2, v*2}} # => {"aa"=>2, "bb"=>4}
View Cycle through colours using jquery
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
View random slicer
#! /usr/bin/ruby
# Saves two random and contiguous cropped images from a larger image
# Written so an image that is too big can be displayed on a dual screen desktop
IMAGE_NAME = "file_small.png"
IMAGE_WIDTH, IMAGE_HEIGHT = IO.read(IMAGE_NAME)[0x10..0x18].unpack('NN')
# # For non-pngs:
# IMAGE_WIDTH, IMAGE_HEIGHT = [12304, 8605]
View Unzoomify
# Hacked from http://github.com/dball/zoomifier/blob/master/lib/zoomifier.rb
require 'fileutils'
require 'open-uri'
require 'rubygems'
require 'rmagick'
require 'rexml/document'
class Zoomify
TILESIZE = 256
@zetter
zetter / Slow queries
Created Feb 8, 2011
Find the slowest queries lines in a rails development log
View Slow queries
file=''
slow = []
File.readlines(file).each do |line|
if line =~ /Load\ \(([\d\.]*)ms\)/
time = $1.to_f
if time >= 500.0
slow << [time, line]
end
end
@zetter
zetter / Bubble Free Click
Created Mar 26, 2011
jquery extension to register click events that won't bubble. Call instead of click(func).
View Bubble Free Click
jQuery.fn.preventDefaultClick = function(f) {
this.click(function(e){
f.call(this, e);
e.preventDefault();
});
return this;
}
@zetter
zetter / deconstruct.markdown
Created Jan 31, 2012
therubygame deconstruct by @czetter
View deconstruct.markdown

therubygame deconstruct by @czetter

This is a deconstuction of matematikaadit's submission to therubygame challenge 5; 'Roman numerals. What are they good IV?'. The goal of the challenge is to take a string representing a roman numeral as input and return the integer that the numeral represents.

matematikaadit currently has the honour of the shortest (by character count) submission for this challenge. At first glance I didn't understand how it worked so I re-wrote and analyzed it until I did.

Making it readable

matematikaadit's original submission:

View ruby-parsing-bug.markdown

A parsing bug in ruby 1.9.2 allowed you to pass blocks to methods preceded by a comma. The bug only affected blocks with do/end and was fixed in 1.9.3.

http://bugs.ruby-lang.org/issues/5540

$ rvm use ruby 1.8.7

[1].inject :+, do |x| 1 end
# => SyntaxError: compile error
@zetter
zetter / SuperReCSS.markdown
Created Mar 21, 2012
Modified ReCSS to reload styles on window focus
View SuperReCSS.markdown

Only tested in chrome, shove this in a bookmarklet:

javascript:void(function(){var r=(function(){var i,a,s;a=document.getElementsByTagName('link');for(i=0;i<a.length;i++){s=a[i];if(s.rel.toLowerCase().indexOf('stylesheet')>=0&&s.href) {var h=s.href.replace(/(&|%5C?)forceReload=\d+/,'');s.href=h+(h.indexOf('?')>=0?'&':'?')+'forceReload='+(new Date().valueOf())}}}); window.addEventListener('focus',r,false)})();

View for_sale.markdown

For Sale

Make me an offer!

Plug & Battery

Books

  • Ten Stories About Smoking
You can’t perform that action at this time.