Skip to content

Instantly share code, notes, and snippets.

View shock's full-sized avatar

Bill Doughty shock

  • Austin, TX
View GitHub Profile
@shock
shock / heredoc.rb
Created July 21, 2016 15:16
String object extensions for handling proper indentation duration HEREDOC interpolation of multi-line strings.
# Created in response to Stack Overflow question:
# http://stackoverflow.com/questions/38504004/interpolate-multiline-string-with-correct-indent
module CoreExtensions
module String
module Heredoc
# Special character to flag lines that are part of a multiline HEREDOC interpolation
# Any character that is not part of the output string will work. Using "\r" because
# it's rarely used in hard-coded strings.
# The following code lets you iterate over large collections of Active Record
# objects without having to load them all at once, thus reducing memory usage.
# It’s allowed me to run cron jobs which iterate over thousands of records
# without getting the cron’d process killed for using too much of a system’s
# resources.
#
# Taken from Flying Machine Studios
# http://www.flyingmachinestudios.com/2008/05/12/chunky-iterator-so-you-dont-have-to-load-all-your-ar-objects-at-once/
# Author: Daniel Higginbotham
@shock
shock / gist:183834
Created September 9, 2009 15:52 — forked from techpeace/gist:180579
Surround a heredoc with quotes and you can continue the code on the same line:
render :status => 404, :text => <<-'EOH' and return unless setup
article not found<br/>
I, as a server, have failed<br/>
https?
EOH
Quotes also give you more freedom/creativity with the terminal ID:
require 'rubygems'
require 'stemmer'
class NaiveBayes
# provide a list of categories for this classifier
def initialize(categories)
# keeps a hash of word count for each category
@words = Hash.new
@total_words = 0
class Spinner
private
@work
@job
def spinner
return(['\\','|','/','-'][@i%4])
end
@shock
shock / gist:120186
Created May 29, 2009 20:17 — forked from jsierles/gist:29838
Find bloating passengers and kill them gracefully.
#!/usr/bin/env ruby
#
# Find bloating passengers and kill them gracefully. Run from cron every minute.
#
# required for passenger since cron has no environment
ENV['HTTPD'] = 'httpd'
MEM_LIMIT = ARGV[0] || 500
class Spinner
private
@work
@job
def spinner
return(['\\','|','/','-'][@i%4])
end