Skip to content

Instantly share code, notes, and snippets.

View alexrothenberg's full-sized avatar

Alex Rothenberg alexrothenberg

View GitHub Profile
@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.
<% sections.each do |section| %>
<li class='<%="active" if active_section? section%>'>
<% = link_to section %>
<% end %>
module Sth
module Generators
class ConfigGenerator < ::Rails::Generators::Base
TXT = '# some inserted text'
desc 'Configs application controller'
def edit_application_Controller
if File.exist?("app/controllers/application_controller.rb")
insert_into_file "app/controllers/application_controller.rb", TXT, :after => "protect_from_forgery"
else
class MyController < UIViewController
def viewDidLoad
super
top_view.addSubview chart_view([[0,0], [100,100],
[10, 20, 20, 15, 35],
["#203a65", "#37537c", "#5b7faa", "#aec2d6", "#aec2d6", "#ffffff"]
)
end
def chart_view(frame, data, colors)
@alexrothenberg
alexrothenberg / app_delegate.rb
Created November 30, 2012 20:10
RubyMotion 1.29 crash - reported as issue #344
# We crash with this definition
# Could the superclass be freed by RubyMotion's automatic memory management???
class Person < Struct.new(:name)
end
# # Everything is fine if we define Person without Struct.new
# class Person
# attr_reader :name
# def initialize(attributes)
# @name = attributes[:name]