Skip to content

Instantly share code, notes, and snippets.

View jeffreyiacono's full-sized avatar

Jeff Iacono jeffreyiacono

  • Sierra.ai
  • San Francisco
  • X @jfi
View GitHub Profile
@jeffreyiacono
jeffreyiacono / triangles-sim.rb
Last active September 30, 2015 13:07
simulation of the math problem: "If you break a straight line randomly in two places, what is the probability that you can form a triangle from the resulting three pieces?"
# simulation to calculate an answer to the question:
#
# "If you break a straight line randomly in two places, what is the probability that you can
# form a triangle from the resulting three pieces?"
#
# usage: ruby ./triangles-sim.rb [trials]
# sample output:
#
# $> ruby ./triangles-sim.rb 1000000
#
@jeffreyiacono
jeffreyiacono / track_spec.rb
Created March 7, 2012 07:03
Track spec for comments / commenter cache retrieval on soundtrak.me
require 'spec_helper'
describe Track do
describe "commenters" do
let(:sleepy) { build(:user, fullname: 'Mr. Sleepy', email: '[email protected]', username: 'sleepy') }
let(:dopey) { build(:user, fullname: 'Mr. Dopey', email: '[email protected]', username: 'dopey') }
let(:doc) { build(:user, fullname: 'Mr. Doc', email: '[email protected]', username: 'doc') }
let(:sleepy_comment) { build(:comment, commenter: sleepy, content: "sleepy's comment") }
let(:dopey_comment) { build(:comment, commenter: dopey, content: "dopey's comment") }
let(:track) { build(:track) }
@jeffreyiacono
jeffreyiacono / make_emailable.rb
Created March 8, 2012 17:40
module that extracts common mailer tasks for soundtrak.me
require 'fileutils'
require 'ostruct'
require 'pony'
require 'active_support'
module MakeEmailable
extend ActiveSupport::Concern
included do
attr_reader :sender, :receiver
@jeffreyiacono
jeffreyiacono / question_spec.rb
Created March 17, 2012 23:06
question spec for Proview
require 'spec_helper'
describe Question do
it { should belong_to(:page) }
it { should belong_to(:survey) }
it { should have_many(:answers) }
describe "default scope" do
let(:first_question) { Question.create(seq: 1) }
let(:second_question) { Question.create(seq: 2) }
@jeffreyiacono
jeffreyiacono / lolrus.js
Created March 18, 2012 04:53
simple normalization of jquery event so offsetX / offsetY can be used cross browsers
/**
* normalizeEvent
*
* Firefox does not implement offsetX, OffsetY, so we have to detect for this an
* manually calculate it ourselves using the pageX, pageY less the event
* target's left offset and right offset
*
* If using a browser that supports offsetX, OffsetY, just return the event,
* don't need to do anything
*/
@jeffreyiacono
jeffreyiacono / question.rb
Created March 20, 2012 18:06
(Partial) Question spec for proview (revisited)
class Question < ActiveRecord::Base
default_scope order("seq ASC")
# decrement seq / page # depending on position and page count
def move_up!
if self.first_on_page?
move_to_previous_page!
else
decrement_seq! && self.survey.renumber_questions!
end
@jeffreyiacono
jeffreyiacono / ladder.sql
Created April 23, 2012 21:49
mysql ladder
CREATE TABLE `ladder` (
`i` int(1) unsigned NOT NULL,
PRIMARY KEY (`i`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO ladder (i) VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);
SELECT l1.i + l2.i * 10 + l3.i * 100 AS sequence
FROM ladder l1
JOIN ladder l2 ON TRUE
@jeffreyiacono
jeffreyiacono / cmds
Created September 6, 2012 02:29
fun with unix programs
$ cat sample | sort | uniq -c
2 bat
4 cat
2 dog
1 gator
$ cat sample | sort | uniq -c | sed "s/^[ \t]*//"
2 bat
4 cat
2 dog
@jeffreyiacono
jeffreyiacono / date-to-datetime-comparison.sql
Created September 11, 2012 00:27
comparison of date to datetimes in MySQL
-- DATETIME to DATE comparison
SELECT CAST('2012-09-09 23:59:59' AS DATETIME) <= DATE('2012-09-10 00:00:00'); -- TRUE :)
SELECT CAST('2012-09-10 00:00:00' AS DATETIME) <= DATE('2012-09-10 00:00:00'); -- TRUE :)
SELECT CAST('2012-09-10 00:00:00' AS DATETIME) <= DATE('2012-09-10 23:59:59'); -- TRUE :)
SELECT CAST('2012-09-10 00:00:01' AS DATETIME) <= DATE('2012-09-10 23:59:59'); -- FALSE :(
-- and a little more proof:
SELECT CAST('2012-09-10 00:00:00' AS DATETIME) = DATE('2012-09-10 00:00:00'); -- TRUE
SELECT CAST('2012-09-10 00:00:00' AS DATETIME) = DATE('2012-09-10 00:00:01'); -- TRUE
SELECT CAST('2012-09-10 23:59:59' AS DATETIME) = DATE('2012-09-10 23:59:59'); -- FALSE
@jeffreyiacono
jeffreyiacono / vim-windowing.md
Created October 1, 2012 06:19
(m/g)vim windowing

(M/G)VIM

Open N windows split horizontally / vertically

-o[N]                Open N windows (default: one for each file)
-O[N]                Like -o but split vertically

you can also specify files:

vim -o3 file1.txt file2.txt file3.txt