Skip to content

Instantly share code, notes, and snippets.

@marcbowes
marcbowes / gist:4017369
Created November 5, 2012 14:12
Dedup posters
require "digest/md5"
require "set"
found = Set.new
def md5sum(filename)
content = File.read(filename)
Digest::MD5.hexdigest(content)
end
@marcbowes
marcbowes / gist:2224748
Created March 28, 2012 08:30
Why I didn't spend $480.11 at ThinkGeek (Livechat transcript)
Marc: Hi. I get this error on checkout: Transaction was declined by processor.
Sade: Hi Marc!
Sade: I will look into this
Sade: What is the order number?
Marc: I'm at checkout
Marc: 35246f7e5
Marc: Earlier I tried via PayPal but that order was rejected too :-(
Sade: Hmm, I am seeing that it being rejected by us.
Sade: Is there anyway you can place the order when you get back to the states?
Marc: I don't live in the states
module NameIdCache
def self.included(base)
m = Module.new
base.all.each do |record|
cache_key = NameIdCache.cache_key(base, record)
Rails.cache.write(cache_key, record.id)
m.__send__(:define_method, record.name) do
Rails.cache.fetch(cache_key) do
module NameIdCache
def self.included(base)
m = Module.new
base.all.each do |record|
m.__send__(:define_method, record.name) do
record.record_id # using #record_id because OpenStruct is iffy about #id
end
end
base.extend(m)
end
@marcbowes
marcbowes / gist:1899731
Created February 24, 2012 09:31
Envelope encryption in Ruby using OpenSSL
# Explanation at http://stackoverflow.com/questions/5212213/ruby-equivalent-of-php-openssl-seal/9428217#9428217
# Implementation
class EnvelopeEncryption
require "openssl"
# This method takes in plaintext and produces ciphertext and an
$ cat Gemfile
source 'http://rubygems.org'
gem "rails", ">=3.0.5"
# To use debugger
# gem 'ruby-debug'
gem "guid"
gem "mysql2"
diff -ur marcbowes-UsingYAML-590c028/lib/using_yaml/array.rb UsingYAML-590c028/lib/using_yaml/array.rb
--- marcbowes-UsingYAML-590c028/lib/using_yaml/array.rb 2010-03-06 22:14:19.000000000 +0200
+++ UsingYAML-590c028/lib/using_yaml/array.rb 2010-03-29 18:05:01.000000000 +0200
@@ -25,5 +25,6 @@
# Load in the extensions for this instance
array.extend(extensions)
+ array
end
end
# This is a cheap stab at demonstrating the behavior of
# Inotify::CREATE. That is, CREATE will trigger inotify events
# immediately upon file creation.
require 'inotify'
require 'fileutils'
# Method which fakes a slow write by sleeping for one second.
def slow_write(filename)
File.open(filename, "w") do |f|