Skip to content

Instantly share code, notes, and snippets.

@jordelver
jordelver / das_download.rb
Created June 16, 2012 22:20 — forked from maca/das_download.rb
Script to download all Destroy All Software screencasts, account needed
#! /usr/bin/env ruby
# usage:
# $ das_download.rb email password [download_directory]
require 'mechanize'
# gem 'mechanize-progressbar'
email = ARGV[0] or raise('Please provide the email address for your account')
password = ARGV[1] or raise('Please provide the password for your account')
path = ARGV[2] || './'
@jordelver
jordelver / gist:2945689
Created June 17, 2012 20:41
Array union and remove duplicates
# Returns a new array by joining and removing duplicates
# http://www.ruby-doc.org/core-1.9.3/Array.html#7C-method
irb(main):001:0> [1, 2, 3] | [1, 2, 3, 4]
=> [1, 2, 3, 4]
@jordelver
jordelver / user_presenter.rb
Created June 17, 2012 20:53
User presenter - Shorter class syntax with method_missing / define_method
require 'rspec'
class Module
def takes(*arg_names)
define_method(:initialize) do |*arg_values|
arg_names.zip(arg_values).each do |name, value|
instance_variable_set(:"@#{name}", value)
end
end
end
@jordelver
jordelver / gist:2962398
Created June 20, 2012 21:37
Diff webpages using sub-shells
colordiff -y <(curl http://www.google.fr | tidy) <(curl http://www.google.co.uk | tidy)
@jordelver
jordelver / gist:3021163
Created June 29, 2012 22:39
Ruby #to_s and #to_str
# http://blog.bigbinary.com/2012/06/26/to_str-in-ruby.html
# https://github.com/rails/rails/commit/188cc90af9b29d5520564af7bd7bbcdc647953ca
class Foo
def to_str
"to_str"
end
def to_s
"to_s"
@jordelver
jordelver / gist:3023440
Created June 30, 2012 11:26
Ruby URL safe random numbers with SecureRandom
# http://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html
# http://coderwall.com/p/kqhlna
require 'securerandom'
SecureRandom.urlsafe_base64(8)
=> "RrFeKH6k-_Y"
=> "VGARhfWcbc4"
=> "e86ga-WnMtA"
@jordelver
jordelver / gist:3028930
Created July 1, 2012 16:47
Ruby trap | kill -INFO <process_id>
# Found here http://tenderlovemaking.com/2012/06/18/removing-config-threadsafe.html
# Add to Rails / Sinatra controller / action
trap(:INFO) do
$stderr.puts "Stuff and things"
end
# Start web process somehow
# Send signal to process
@jordelver
jordelver / after.rb
Created July 6, 2012 16:03 — forked from mperham/after.rb
Thread-friendly shared connection
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
@jordelver
jordelver / gist:3071912
Created July 8, 2012 17:25
Single Responsibility Principle
class AttributeCache
def fetch(key)
get(key) || yield
end
def get(key)
{ 'title' => 'Cached' }
end
end
@jordelver
jordelver / gist:3073101
Created July 8, 2012 22:06
Set the Mac OS X SOCKS proxy on the command line

Set the Mac OS X SOCKS proxy on the command line

a.k.a. what to do when your ISP starts blocking sites :(

Set the SOCKS proxy to local SSH tunnel

networksetup -setsocksfirewallproxy "Ethernet" localhost 8080

To clear the domain and port