Skip to content

Instantly share code, notes, and snippets.

View todd-a-jacobs's full-sized avatar

Todd A. Jacobs todd-a-jacobs

View GitHub Profile
@todd-a-jacobs
todd-a-jacobs / durstenfeld_shuffle.coffee
Created May 6, 2011 16:38 — forked from dpritchett/durstenfeld_shuffle.coffee
durstenfeld array shuffle in coffeescript
# See http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
# written as one-liners because my REPL didn't like multiline functions this morning
# tested on CoffeeScript v1.0.1
input = [0 .. 7]
swap = (input, x, y) -> [input[x], input[y]] = [input[y], input[x]]
rand = (x) -> Math.floor(Math.random() * x)
durst = (input) -> swap(input, i, rand(i)) for i in [input.length - 1 .. 1]
"""
@todd-a-jacobs
todd-a-jacobs / gist:2203978
Created March 26, 2012 08:52
Install RVM with Puppet
exec { '/bin/bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)':
provider => shell,
creates => '/home/vagrant/.rvm',
cwd => '/tmp',
user => 'vagrant',
}
@todd-a-jacobs
todd-a-jacobs / flock_example.rb
Created March 8, 2013 13:43
Ruby requires a Bitwise OR to make non-blocking File#flock work.
f1 = File.open('foo', File::RDWR|File::CREAT, 0644)
f1.flock(File::LOCK_EX)
# => 0
f2 = File.open('foo', File::RDWR|File::CREAT, 0644)
f2.flock(File::LOCK_NB|File::LOCK_EX)
# => false
f1.close; f2.close
# => nil
@todd-a-jacobs
todd-a-jacobs / null_object.rb
Created May 29, 2013 18:56
Basic null object.
class NullObject < BasicObject
def method_missing(*) end
def respond_to?(*) true end
end
The regex patterns in this gist are intended to match any URLs,
including "mailto:foo@example.com", "x-whatever://foo", etc. For a
pattern that attempts only to match web URLs (http, https), see:
https://gist.github.com/gruber/8891611
# Single-line version of pattern:
(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
The regex patterns in this gist are intended only to match web URLs -- http,
https, and naked domains like "example.com". For a pattern that attempts to
match all URLs, regardless of protocol, see: https://gist.github.com/gruber/249502
# Single-line version:
(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|s
# username = "my_username"
# pwd = "my_password"
# target_path = "my_target_path"
# saving auth cookie
system %Q{wget --save-cookies /tmp/cookie.txt --keep-session-cookies --post-data "username=#{username}&password=#{pwd}" -O - \
https://rubytapas.dpdcart.com/subscriber/login?__dpd_cart=d08391e6-5fe2-4400-8b27-2dc17b413027}
(25..600).each do |i|
@todd-a-jacobs
todd-a-jacobs / any_vs_intersection.rb
Last active August 29, 2015 14:06
Benchmark Enumerable#any? vs. Array#&
require 'benchmark'
ITERATIONS = 100_000
Benchmark.bmbm do |x|
x.report('Array#&') do
ITERATIONS.times do
([2, 6, 13, 99, 27] & [6, 13]).any?
end
end
x.report('Enumerable#any?') do
@todd-a-jacobs
todd-a-jacobs / kernel_rand_with_range.rb
Created September 22, 2014 20:26
Testing Kernel#rand with a Range of FIXNUM_MIN..FIXNUM_MAX
FIXNUM_MAX = (2**(0.size * 8 -2) -1)
FIXNUM_MIN = -(2**(0.size * 8 -2))
1_000_000.times do
r = rand(FIXNUM_MIN..FIXNUM_MAX)
fail if r > FIXNUM_MAX or r.class != Fixnum
fail if r < FIXNUM_MIN or r.class != Fixnum
end
@todd-a-jacobs
todd-a-jacobs / xcode.exp
Created September 27, 2014 05:49
Accept xcodebuild license agreement non-interactively.
#!/usr/bin/expect -f
# vim: sw=4:et:ft=expect:fdm=indent
set timeout -1
log_user 0
spawn sudo xcodebuild -license
match_max 100000
expect {
"Hit the Enter key" {
send -- "\r"