Skip to content

Instantly share code, notes, and snippets.

View alexrothenberg's full-sized avatar

Alex Rothenberg alexrothenberg

View GitHub Profile
#!/usr/bin/env ruby
require 'fileutils'
def analyze
Dir.glob("public/dist/**/*.{js,css}").reduce({}) do |hash, filename|
filename =~ /public\/dist\/([^-]*)(-.{10})?\.(.*)/
name = "#{$1}.#{$3}"
# puts name
hash[name] = File.new(filename).size
signups = cuco_session.course_signups.includes(:course_role, :course).where(person: family.people)
count = signups.where(person_id: person.id).joins(:course).where(courses: { period_id: period.id }).count
##
x = cuco_session.course_signups
y = x.includes(:course_role, :course)
signups = y.where(person: family.people)
a = signups.where(person_id: person.id)
// What should we call the things under each day?
// Its kinda like a recurring event but not quite
// "WeeklyScheduleEventThingy"? We can do better!
// http://delorean/schedules?staff_member_uid=asdfasdfasfd
// create, update, index, show
{
"schedule": {
"start_date": "2016-01-01",
-(void)someBigMethod {
// create a screenshot
CALayer * screenLayer = [[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] layer];
CGFloat const shotScale = ([[UIScreen mainScreen] scale] / 2.0);
UIGraphicsBeginImageContextWithOptions(screenLayer.bounds.size, YES, shotScale);
[screenLayer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// put the screenshot in a layer
@alexrothenberg
alexrothenberg / ui_view.rb
Created November 21, 2013 16:23
Convenience methods for dealing with coordinates in RubyMotion
class UIView
def top
frame.origin.y
end
def left
frame.origin.x
end
def height
frame.size.height
end
@alexrothenberg
alexrothenberg / surpassing_problem.prolog
Last active December 15, 2015 00:19
This is the first prolog code I've written in 20 years. I used to be very into it but that was a long time ago.
% A brute force algorithm to solve the Surpassing Problem posed by Martin Rem in 1988
%
% By definition, a surpasser of an element of an array is a greater element to the right,
% so x[j] is a surpasser of x[i] if i < j and x[i] < x[j]. The surpasser count of an element
% is the number of its surpassers. For example, the surpasser count of the string GENERATING
% are given by
%
% G E N E R A T I N G
% 5 6 2 5 1 4 0 I 0 0
% The maximum surpasser count is 6. The first occurrence of the letter E has six surpassers,
# Ruby Implementation of lazy array for memoized fib
# Inspired by Pat Shaughnessy's talk at http://bostonrb.org/presentations/month/March-2013
# Define a LazyArray class that caches results
# The cache is valid as long as we use it functionally meaning that
# the fn has no side effects and always returns the same output for the same input
class LazyArray
def initialize(fn)
@fn = fn
@cache = {}
def fib(x)
puts "fib(#{x})"
return 1 if [0,1].include? x
return fib(x-1) + fib(x-2)
end
cache = (0..Float::INFINITY).lazy.map {|x| fib(x) }
class << cache
def [](n)
take(n).force.last
@alexrothenberg
alexrothenberg / security-audit
Created February 25, 2013 14:44
1. gem install bundler-audit 2. run this script 3. learn what you need to upgrade
#! /usr/bin/env ruby
Dir['/srv/*/current'].each do |dir|
if File.exist?("#{dir}/Gemfile.lock")
puts dir
puts `cd #{dir} && bundle-audit`
end
end
# =============================
# Test case showing what we expect to happen
a1 = 'z'
b1 = 'z'
a = [a1]
b = [b1]
# array '-' https://github.com/ruby/ruby/blob/trunk/array.c#L3806
# It compares elements using their #hash and #eql? methods for efficiency.