Skip to content

Instantly share code, notes, and snippets.

View coreyward's full-sized avatar

Corey Ward coreyward

View GitHub Profile
@coreyward
coreyward / README
Created October 14, 2010 21:52
Ruby wrapper to `mysqldump`. Intended to make setting up automatic cronjob backups easier by avoiding bash scripting.
Usage:
ruby mysql_backup.rb db:name [user:your_username] \
[password:your_password] \
[host:your_hostname] \
[file:output_file.sql]
Feel free to use, improve, etc. I'm new to Ruby, so excuse anything that makes your palm slap your forehead. ;)
@coreyward
coreyward / check_imap_mail.rb
Created November 27, 2010 19:31
Fetch emails from a Gmail account via IMAP for processing
# Via http://blog.ethanvizitei.com/2008/06/using-ruby-for-imap-with-gmail.html
imap = Net::IMAP.new(@config['server'],@config['port'],true)
imap.login(@config['username'], @config['password'])
imap.select('INBOX')
imap.search(["NOT", "DELETED"]).each do |message_id|
MailFetcher.receive(imap.fetch(message_id, "RFC822")[0].attr["RFC822"])
imap.store(message_id, "+FLAGS", [:Deleted])
end
imap.logout()
@coreyward
coreyward / range.random.rb
Created December 10, 2010 03:14
Easy random numbers (or letters) from a range
class Range
def pick
if min.is_a? Fixnum
rand(count) + min
else
self.to_a[rand(count)]
end
end
alias :random :pick
end
@coreyward
coreyward / Ruby#inject.rb
Created January 16, 2011 03:50
An implementation of Ruby's inject method, written in Ruby (for learning/teaching purposes)
class Array
def my_inject (accumulator = 0, &block)
self.each do |item|
accumulator = block.call(accumulator, item)
end
accumulator
end
end
@coreyward
coreyward / Explicit Block vs Symbol#to_proc.rb
Created February 16, 2011 22:17
A quick benchmark to confirm/dispel performance concerns over using `.map(&:to_i)` instead of a literal block (`.map { |x| x.to_i }`).
require 'benchmark'
n = 100000
a = %w{ 1 2 3 4 5 6 7 8 9 0 }
Benchmark.bmbm do |b|
b.report 'Explicit Block' do
n.times do
a.map { |x| x.to_i }
end
@coreyward
coreyward / cloudfiles_test.rb
Created February 23, 2011 01:25
Test cloudfiles gem for thread safety through trial and error...
require 'rubygems'
require 'cloudfiles'
class Hash
def reverse_merge(other_hash)
other_hash.merge(self)
end
end
class Cloud
@coreyward
coreyward / gist:937889
Created April 22, 2011 22:56
Quickly add a git remote mirror
# because I always forget the magic sequence of commands…
ssh host 'git init --bare /var/src/repo.git'
git remote add mirror --mirror host/var/src/repo.git
git push mirror # tada
@coreyward
coreyward / debounce.coffee
Created July 29, 2011 23:33
Debounce in CoffeeScript
# Debounce and SmartResize (in jQuery) ported to CoffeeScript
# debouncing function from John Hann
# http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
debounce = (func, threshold, execAsap) ->
timeout = false
return debounced = ->
obj = this
args = arguments
@coreyward
coreyward / application_controller.rb
Created July 2, 2012 20:26
If we quit treating <head> as HTML output and started thinking about it more like we do Rack middleware or HTTP headers (i.e. more object-oriented), I think we could gain a lot in the way of flexibility and structure, especially in complex applications.
class ApplicationController < ActionController::Base
head << meta charset: :utf8
head << title 'My Application'
head << stylesheet :application
end