Skip to content

Instantly share code, notes, and snippets.

View levicole's full-sized avatar

Levi Cole levicole

  • Kolide
  • Nashville, TN
View GitHub Profile
@levicole
levicole / NSImageExtensions.swift
Last active April 25, 2016 05:04
Draws Image in a target size. If it's aspect ratio won't fit in the size, it's cropped.
extension NSImage {
func drawInTargetSize(targetSize: NSSize) -> NSImage? {
let aspectRatio = max(targetSize.width/self.size.width, targetSize.height/self.size.height)
let newSize = NSSize(width: self.size.height * aspectRatio, height: self.size.height * aspectRatio)
var newRect = NSRect(origin: NSPoint(x: 0, y: 0), size: newSize)
let ctx = NSGraphicsContext()
guard let resizedImage = self.CGImageForProposedRect(&newRect, context: ctx, hints: nil) else { return nil }
let yOffset = (newSize.height > targetSize.height) ? floor((newSize.height - targetSize.height)/2.0) : 0
def knapsack(weights, values, max_weight)
raise ArgumentError nil if weights.length != values.length
matrix = (1..values.length).collect { || Array.new(max_weight + 1, 0) }
matrix.each_with_index do |row, i|
weight = weights[i]
value = values[i]
row.each_with_index do |current_value, j|
previous = matrix[i - 1][j].to_i
if j >= weight
row[j] = new = [matrix[i - 1][j - weight].to_i + value, previous].max
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
<%# form_for @model do |f| -%>
<!-- this is an HTML comment -->
<%# end -%>
<%# link_to blah, blah %>
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi

attr_tracker

As a Developer, I want a generator that generates the appropriate migration for tracking attribute changes

When writing a gem that requires a database migration, you should provide a generator for your users so that they don't have to write the migration themselves. Some questions you should ask yourself:

  • What will your data model look like?
  • What should the table be named?
  • Should we allow our users to name the table themselves?
  • Should the generator generate the model as well? Or should we keep the model in the gem?
CONVERSION_HASH = {
1 => "one",
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
7 => "seven",
8 => "eight",
9 => "nine",
= form_for Activity.new do |f|
= f.hidden_field :activity_type, "shower"
= f.text_field :per_use
= form_for Activity.new do |f|
= f.hidden_field :activity_type, "bath"
= f.text_field :per_use
= form_for Activity.new do |f|
= f.hidden_field :activity_type, "toothbrush"
ROMAN_MAP = {
1 => "I",
4 => "IV",
5 => "V",
9 => "IX",
10 => "X",
40 => "XL",
50 => "L",
90 => "XC",
100 => "C",
(defn elipse-maker [n]
(fn [p]
(str/join " " (conj (vec (take n (str/split p #"\W+"))) "..."))))