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 / string_prototype_times.js
Created November 29, 2012 08:45
add #times to String prototype
​String.prototype.times = function(n) {
// join checks its first argument for a length property, so we supply it with one.
// what normally happens is something like this:
//
// [1, 2, 3].length //=> 3
// [1, 2, 3].join(", ") //=> "1, 2, 3"
//
// so the following is equivalent ...
//
// Array.prototype.join.call([1, 2, 3], ", ") //=> "1, 2, 3"
@jeffreyiacono
jeffreyiacono / extending_modules.rb
Created November 29, 2012 08:13
fun with extending ruby modules ... or "how do we get mod === to pass without actually being an ancestor of the type"
class SqlQuery; end
query = SqlQuery.new
puts "String === query is #{String === query}"
#=> String === query is false
String.extend Module.new {
def ===(obj)
obj.instance_of?(SqlQuery) or super
@jeffreyiacono
jeffreyiacono / define-log-property.js
Created November 29, 2012 07:38
define a log property on the window (or global) obj in js
// via https://gist.github.com/4080907
Object.defineProperty(window, 'log', {
set: function(args) {
console.log.apply(console, Array.isArray(args) ? args : [args]);
this.value = args
},
get: function() {
return this.value + " from .get";
}
});
@jeffreyiacono
jeffreyiacono / name_ref.py
Created November 19, 2012 06:04
call by reference (ruby / python)
some_guy = 'Fred'
first_names = []
first_names.append(some_guy)
another_list_of_names = first_names
another_list_of_names.append('George')
some_guy = 'Bill'
@jeffreyiacono
jeffreyiacono / arrow_lambdas.rb
Created November 19, 2012 04:10
new ruby arrow lambda syntax
# fun with the new lambda syntax
adder = ->(a, b) { a + b }
subtr = ->(a, b) { a - b }
divdr = ->(a, b) { a / b }
multr = ->(a, b) { a * b }
puts "adder.(4, 2) = #{adder.(4, 2)}"
puts "subtr.(4, 2) = #{subtr.(4, 2)}"
puts "divdr.(4, 2) = #{divdr.(4, 2)}"
puts "multr.(4, 2) = #{multr.(4, 2)}"
@jeffreyiacono
jeffreyiacono / iacono_epoch.rb
Created November 19, 2012 03:58
A simple script that tells you if a passed in date is pre or post Iacono
# Date.new will be the beginning of time
# Date::Infinity.new will be the end of time
require 'date'
case Date.parse(ARGV[0])
when Date.new..Date.new(1983, 1, 15)
puts "pre-Iacono"
when Date.new(1983, 1, 15)..Date::Infinity.new
puts "post-Iacono"
end
@jeffreyiacono
jeffreyiacono / fun_with_utf-8.rb
Created November 19, 2012 03:41
Ruby fun with UTF-8
# encoding: UTF-8
class Weather
def ☃
"I'm a snowman!"
end
def ☁
"Looking like a cloudy day"
end
@jeffreyiacono
jeffreyiacono / gethub.rb
Created November 11, 2012 22:18
Get github information for a user
require 'net/http'
require 'uri'
require 'io/console'
puts "Enter in a github username:"
gh_username = gets.chomp
puts "Enter in the github user password (hidden):"
gh_password = STDIN.noecho(&:gets).chomp
uri = URI.parse('https://api.github.com/user')
@jeffreyiacono
jeffreyiacono / fun_with_bash_special_chars.sh
Created November 8, 2012 18:27
fun with bash special chars
#!/bin/sh
echo "you passed me $# args"
echo "pid = $$"
echo "and I am $0"
for a in $@
do
echo $a
done
@jeffreyiacono
jeffreyiacono / flocking
Last active October 12, 2015 12:58
a sample script that sleeps
# round 1! => nonblocking example
[process a] $ flock -en sleeper.lock ./sleeper.sh
time to sleep ...
[process b] $ flock -en sleeper.lock ./sleeper.sh
# ^ will exit b/c of n switch: "Fail (with an exit code of 1)
# rather than wait if the lock cannot be immediately acquired.
... waking up # <= process a returning
# round 2! => blocking example