Skip to content

Instantly share code, notes, and snippets.

View moserrya's full-sized avatar

Ryan Moser moserrya

View GitHub Profile
item = Item.find 328
item.transaction do
item.feelings.create!(size: 'huge')
item.update_attributes!(state: "sold")
end
# item.update_attributes! fails a validation, which we rescue. new feeling that we created is rightly rolled back
item.save #saves the feelings record that was rolled back as part of our transaction!
(ns erdos-c.core
[:require [yokogiri.core :refer :all]])
(def client (make-client :javascript false))
(defn page [uri]
(get-page client (str "http://en.wikipedia.org" uri)))
(defn link-snippets [uri]
(xpath (page uri) "//a"))
@moserrya
moserrya / clean_chain.rb
Created January 9, 2013 17:07
A solution to the ZeroCater math-y challenge that is capable of finding the fifth clean chain of primes.
# Take a sequence of N numbers. We'll call the sequence a "Clean Chain of length N"
# if the sum of the first N - 1 numbers is evenly divisibly by the Nth number.
# For example, the sequence [2, 4, 6, 8, 10] forms a clean chain of length 5,
# since 2 + 4 + 6 + 8 = 20, which is divisible by 10, and the sequence has 5 numbers.
# The first clean chain formed out of the sequence of primes is simply [2], with length 1.
# The second is [2, 3, 5] with length 3.
# The third is [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
# 53, 59, 61, 67, 71], with length 20
@moserrya
moserrya / gist:4671370
Created January 30, 2013 07:14
best fizzbuzz ever
(1..100).map {|i| (fb = [["Fizz"][i%3],["Buzz"][i%5]].compact.join).empty? ? i : fb}

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@moserrya
moserrya / gist:5053341
Created February 28, 2013 01:06
lots of html
<!DOCTYPE html>
<html lang="en">
<head>
<!--
normalize.css removes cross-browser differences in defaults, e.g.,
differences in how form elements appear between Firefox and IE
See: http://necolas.github.com/normalize.css/
-->
<link rel="stylesheet" href="normalize.css">
@moserrya
moserrya / rpn_calc.rb
Last active December 14, 2015 18:39
Stabby RPN
class RPNCalculator
def initialize
@special_cases = [->(op, arr) {arr.include?('0') && op == '*' && '0'},
->(op, arr) {arr.first == ('0') && op == '/' && '0'},
->(op, arr) {arr.include?('1') && op == '*' && (arr-['1']).first},
->(op, arr) {arr.include?('0') && op =~ /[+-]/ && (arr-['0']).first}]
end
def evaluate(*inputs)
class CalculatesRoute
def self.calculate(points)
remaining_points = points
route = []
route << remaining_points.slice!(0)
until remaining_points == [] do
next_point = shortest_distance(route.last, remaining_points)
route << remaining_points.slice!(remaining_points.index(next_point))
require_relative 'geocoder'
require 'shoulda-matchers'
require 'geocoder'
require 'debugger'
class ActivityCluster
MAX_ROUTE_LENGTH = 4
MAX_DISTANCE = 20
@moserrya
moserrya / justify.js
Last active December 16, 2015 10:58
Justify strings to a given width. Works in-place without breaking the string into an array.
function justify(str, width) {
var whitespace = ' ';
while (str.length < width) {
var oldLength = str.length;
var spacing = new RegExp("(\\w)" + whitespace + "(\\w)");
str = str.replace(spacing, '$1 ' + whitespace + '$2');
if (oldLength === str.length) {
whitespace += ' ';
}
}