Skip to content

Instantly share code, notes, and snippets.

@codeincontext
codeincontext / RandomRecord.rb
Created September 7, 2010 21:05
The cleanest way of selecting a random record from an ActiveRecord table.
module ActiveRecord
class Base
def self.random(conditions = nil)
c = count(:conditions=>conditions)
unless c == 0
first(:conditions=>conditions, :offset =>rand(c))
end
end
end
end
@codeincontext
codeincontext / itunes-podcast-categories.yml
Created February 7, 2011 12:50
A list of the current iTunes podcast categories - in YAML format
- Arts:
- Design
- Fashion & Beauty
- Food
- Literature
- Performing Arts
- Spoken Word
- Visual Arts
- Business:
- Business News
@codeincontext
codeincontext / get_twitter_ids.rb
Created May 17, 2011 13:22
Dodgy script to fetch twitter IDs from OAuth tokens using threads
# script/runner lib/get_twitter_user_ids.rb
class ErrorFourOhOne < Exception; end
class ThreadWorker < Thread
def initialize(twitter_auths, ids)
@ids = ids
@twitter_auths = twitter_auths
super do
get_id_for(@twitter_auths.pop) while twitter_auths.present?
@codeincontext
codeincontext / mail_generator.rb
Created June 21, 2011 15:17
A script to generate all of our ActionMailer emails
# rails runner lib/mail_generator.rb
OUTPUT_DIR = 'mail_examples'
`rm -rf #{OUTPUT_DIR}/`
`mkdir #{OUTPUT_DIR}/`
@methods = UserMailer.instance_methods(false).map(&:to_sym)
def generate(method, *params)
mail = UserMailer.send(method, *params)
@codeincontext
codeincontext / gist:1040315
Created June 22, 2011 15:18
'method?' behaviour on all ruby objects
class Object
def method_missing(method, *args, &block)
if (m = method.to_s).ends_with? '?'
!!send(m.chop, *args, &block)
else
super
end
end
end
@codeincontext
codeincontext / gist:1189597
Created September 2, 2011 19:24
mixing two audioboos with <audio>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
// file://localhost/Users/skattyadz/code/audio/index.htm?boo1=157217&boo2=3113
$(document).ready(function(){
var audio1 = document.createElement('audio');
var audio2 = document.createElement('audio');
audio1.volume=0.5;
@codeincontext
codeincontext / gist:1226678
Created September 19, 2011 14:53
Android square layout
package com.cube.vision;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareLayout extends LinearLayout {
public SquareLayout(Context context) {
super(context);
}
@codeincontext
codeincontext / websocket-server.rb
Created September 21, 2011 10:34
example of subscribing incoming websocket connections to redis using eventmachine
require 'rubygems'
require 'eventmachine'
require 'em-websocket'
require 'json'
require 'em-hiredis'
EventMachine.run do
@channel = EM::Channel.new
@codeincontext
codeincontext / websocketNotifier.js
Created September 21, 2011 10:35
A javascript class to subscribe via websocket to a redis channel
function WebsocketNotifier() {
this.connect = function() {
var socket = new WebSocket('ws://10.0.1.9:8080')
that = this;
socket.onmessage = function(mess) {
console.log("eventmachine -> " + mess.data);
$(that).trigger('updatesAvailable');
};
};
@codeincontext
codeincontext / gist:1268425
Created October 6, 2011 19:41
iOS 5 tips and tricks

Note: you can find a more involved review of iOS5 here: http://modmyi.com/content/5532-ios-5-released-full-review.html

iOS 5 Quick Tips

So you've just upgraded your iPhone to the new version (iOS 5), this is a little post to help you get the most out of the new shiny features available to you.

  • Your phone will automatically sync over wifi when you're at home. You don't have to plug it in or wait for it to finish.

  • Messages to other iPhone and iPod touch users will now go over wifi if possible. These will come up in a different colour.