Skip to content

Instantly share code, notes, and snippets.

@gmanley
gmanley / reimplement_group_by.rb
Created March 15, 2013 21:08
Just to illustrate how you would go about reimplementing group_by.
require 'rspec'
class Array
def custom_group_by(&block)
results = {}
each do |e|
(results[block.call(e)] ||= []) << e
end
@gmanley
gmanley / sphinx.rb
Last active December 15, 2015 17:18
Same as: https://raw.github.com/mxcl/homebrew/82cf700/Library/Formula/sphinx.rb but for sphinx 0.9.9 which some things require.
require 'formula'
class Libstemmer < Formula
# upstream is constantly changing the tarball,
# so doing checksum verification here would require
# constant, rapid updates to this formula.
head 'http://snowball.tartarus.org/dist/libstemmer_c.tgz'
homepage 'http://snowball.tartarus.org/'
end
@gmanley
gmanley / 1_parsing_numeral_dates.rb
Last active December 16, 2015 18:39
Ruby code snippets from my work in progress media file parser/renamer.
year = /(?<year>(20)?(07|08|09|10|11|12|13))/
month = /(?<month>0[1-9]|1[012])/
day = /(?<day>0[1-9]|\.[1-9]|[12][0-9]|3[01])/
NUMERAL_DATE_REGEX = /#{year}(?<separator>\.|\-|\/|_)?#{month}(\k<separator>)?#{day}/
NUMERAL_DATE_FORMAT = '[%y.%m.%d]'
# ...
def parse_numeral_date
if match = NUMERAL_DATE_REGEX.match(file_name)
# app/validators/uuid_validator.rb
class UUIDValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
UUIDTools::UUID.parse_hexdigest(value)
rescue ArgumentError
UUIDTools::UUID.parse(value)
rescue ArgumentError
record.errors[attribute] << (options[:message] || "is not a valid UUID")
end
def initialize(model = nil, mounted_as = nil)
if model
self.class.class_eval do
model.image_types.each do |type|
version type.name.to_sym do
process :resize_to_fill => [type.crop_x, type.crop_y]
# process :store_geometry
end
end
end
@gmanley
gmanley / turbo_dev.rb
Last active December 19, 2015 19:18
Middleware that causes static asset requests to bypass rails altogether. This can make requests a lot faster. From 4.5s to 1.5s in some apps. Only relevant in development environments as production already does this.
# Extracted from http://git.io/HeGfdA
# Full credit to https://github.com/SamSaffron
#
# Middleware that causes static asset requests to bypass rails altogether. This
# can make requests a lot faster. From 4.5s to 1.5s in some apps. Only relevant
# in development environments as production already does this.
#
# Put this file in lib/middleware and add the following code to your
# development.rb environment file:
#
deals = ['uuid1', 'uuid2', 'uuid3'].map do |uuid|
deal_info = double()
deal_info.stub_chain(:[], :uuid) { uuid }
deal_info
end
validates :full_url,
presence: true,
format: { with: URI.regexp(%w(http https)),
message: "The 'url' parameter is invalid!" }
#!/usr/bin/ruby
# -*- encoding: binary -*-
$stdout.sync = $stderr.sync = true
# this is used to show or watch the number of active and queued
# connections on any listener socket from the command line
require 'aggregate'
require 'csv'
require 'raindrops'
require 'optparse'
require 'benchmark'
begin
require 'active_support/values/time_zone'
rescue LoadError
puts 'Please do `gem install active_support`'
exit(1)
end
def benchmark_hash(hash, key, inverted_hash)
Benchmark.bmbm do |x|