Skip to content

Instantly share code, notes, and snippets.

View jmazzi's full-sized avatar

Justin Mazzi jmazzi

View GitHub Profile
@jmazzi
jmazzi / countdown_prompt.rb
Created July 17, 2009 15:46 — forked from avdi/countdown_prompt.rb
a command line prompt with timeout and countdown
# :PUBLISHER: markdown, shell, { command: 'rdiscount' }
# :BRACKET_CODE: '[ruby]', '[/ruby]'
# :TEXT:
#
# Have you ever started a long operation and walked away from the computer, and
# come back half an hour later only to find that the process is hung up waiting
# for some user input? It's a sub-optimal user experience, and in many cases it
# can be avoided by having the program choose a default if the user doesn't
# respond within a certain amount of time. One example of this UI technique in
# the wild is powering off your computer - most modern operating systems will
(* Check if iTunes is running and grab current song / artist / album *)
tell application "System Events"
if exists process "iTunes" then
tell application "iTunes"
if player state is playing then
set current_song to (name of current track) as Unicode text
set current_artist to (artist of current track) as Unicode text
if current_artist is equal to "" then set current_artist to "Unknown"
set music_playing to "♫ " & current_song & " - " & current_artist
end if
class CallLog < ActiveRecord::Base
def self.connect_db(extension)
# validate extension
npa = extension[1..3]
nxx = extension[4..6]
return self if self.table_name == "cdr_#{nxx}"
master = {"username"=>"root", "adapter"=>"mysql", "host"=>"localhost", "password"=>"", "database"=>"cdr_#{npa}"}
#!/usr/bin/env ruby
puts "looking for the gems to upgrade..."
gem_info = Struct.new(:name, :version)
to_reinstall = []
Dir.glob('/Library/Ruby/Gems/**/*.bundle').map do |path|
path =~ /.*1.8\/gems\/(.*)-(.*?)\/.*/
name, version = $1, $2
bundle_info = `file path`
to_reinstall << gem_info.new(name, version) unless bundle_info =~ /bundle x86_64/
end
@jmazzi
jmazzi / carbon.md
Created December 4, 2009 03:00 — forked from defunkt/carbon.md

Vim

autocmd BufWritePre * :%s/\s\+$//e

Emacs

(add-hook 'before-save-hook 'delete-trailing-whitespace)

Textmate

# Newbie Programmer
def factorial(x)
if x == 0
return 1
else
return x * factorial(x - 1)
end
end
puts factorial(6)
puts factorial(0)
beans = [:arabica, :robusta]
beans.select { |type| type == :arabica }
beans.flatten!
beans.compact!
espresso = beans.inject(:steam) {}
frapuccino = ([[:milk]] << [espresso]).freeze
frapuccino.take_while { |cup| cup.any? }

Rails 2.3.5 on App Engine (DataMapper)

Do not use rvm (or install and run from JRuby). The google-appengine gem must install into your system MRI. The appengine-sdk gem includes a complete Java app server. We bootstrap Java from MRI, then your app runs inside a servlet container (with access to all the APIs) using the version of JRuby installed into each app.

We assumed Rails 2 would never work without rubygems, and we committed to gem bunlder for JRuby on App Engine, so we were waiting for Rails 3. Fortunately, Takeru Sasaki was able to patch the Rails 2.3.5 calls to rubygems, and now we have it working. Rails 2.3.5 currently spins up several seconds faster than Rails 3, and just a few seconds behind Sinatra.

See the TinyDS version also: gist.github.com/269075

Install the Development Environment

# estimate the duration of an ALTER TABLE statement in mysql
# this code estimates the worst case, as most of the time, an ALTER will result in a table that is smaller than the original
percentage = 0
interval = 10
while(true) do
sleep(interval)
new_percentage = (File.size('#sql-5316_1a.ibd')*1.0 / File.size('moves.ibd')*1.0)
change = new_percentage - percentage
intervals = 1.0/change
@jmazzi
jmazzi / sort.rb
Created June 22, 2010 18:23 — forked from itspriddle/sort.rb
# Sort messages in this folder
#
# Example:
# Before Sort: msg0002, msg0005, msg0010, msg0020
# After Sort: msg0000, msg0001, msg0002, msg0003
def sort!
temp = []
messages.each_with_index do |message, index|
# message.files is an array of files [msg0000.{txt,wav}]
message.files.each do |file|