Skip to content

Instantly share code, notes, and snippets.

$ heroku pg:cachehit --remote production
name | ratio
----------------+------------------------
index hit rate | 0.99929808820146663449
cache hit rate | 1.00
# Requires Ruby 1.9 and OS X
# The letters in your game... include repeats
game_letters = %w()
# Words that have been played already
played = %w()
def to_hash(letters)
letters.inject({}) do |h,l|
h[l] ||= 0
heroku pgbackups:capture --remote production && heroku pgbackups:restore DATABASE `heroku pgbackups:url --remote production` --remote staging
@r38y
r38y / default.rb
Created March 1, 2013 20:04
Recipe to wrap EY backups with a snitch.
# set backup interval
# cron_hour = if node[:backup_interval].to_s == '24'
# "1" # 0100 Pacific, per support's request
# # NB: Instances run in the Pacific (Los Angeles) timezone
# elsif node[:backup_interval]
# "*/#{node[:backup_interval]}"
# else
# "1"
# end
@r38y
r38y / application_helper.rb
Created March 2, 2013 16:57
Rendering Markdown
module ApplicationHelper
def md(text)
text = text || ''
# in very clear cases, let newlines become <br /> tags
text.gsub!(/^[\w\"\<][^\n]*\n+/) do |x|
x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
end
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, space_after_headers: true)
markdown.render(text).html_safe
end
@r38y
r38y / fix_scrolling.coffee
Last active December 15, 2015 13:29
Fixes the bouncing viewport when scrolling in full-window javascript apps. You mostly see this in Chrome (?).
# Uses https://github.com/brandonaaron/jquery-mousewheel
fixScrolling: (elem) ->
$('body').on 'mousewheel', elem, (e, d) ->
$elem = $(elem)
height = $elem.height()
scrollHeight = $elem.get(0).scrollHeight
if (d < 0 && (this.scrollTop is (scrollHeight - height)) || (d > 0 && this.scrollTop is 0))
e.preventDefault()
@r38y
r38y / open-url.rb
Created May 2, 2013 16:34
Handling files from the internet
require 'open-uri'
require 'securerandom'
url = 'http://bntp-production.imgix.net/uploads/photos/file/14/4a902ce7e2.jpg'
file = open(url)
puts file.class
# at this point you can read from it and use it just like
# the file you read from disk
@r38y
r38y / run_dms.rb
Last active December 18, 2015 10:19
A class for wrapping DMS.
require 'net/https'
require 'uri'
class RunDMS
def self.snitch(id, options={}, &block)
dead_man = new(id, options)
block.call
dead_man.snitch
end
@r38y
r38y / handle_funky_formats.rb
Created June 14, 2013 04:20
It handles the case where there is a funky format and a template for it can't be found.
module HandleFunkyFormats
extend ActiveSupport::Concern
included do
rescue_from ActionView::MissingTemplate do
if params[:format]
redirect_to format: nil
end
end
end
@r38y
r38y / address.rb
Created August 5, 2013 14:32
Example refactoring of the class in Griddler that parses an email address.
module Griddler
class Address
def initialize(address)
@address = address
end
def some_method_name(type=:hash)
if type == :hash
parsed_address
else