Skip to content

Instantly share code, notes, and snippets.

@sionide21
sionide21 / 0_enumerator.rb
Last active August 29, 2015 14:10
Lazy enumerators aren't magic
class Benerator
def initialize(&block)
@block = block
end
def next
as_array.shift
end
def as_array
@sionide21
sionide21 / alias.rb
Created August 12, 2014 19:40
The dangers of `alias` in ruby
class Parent
def foo
"Parent#foo"
end
alias :bar :foo
end
class Child < Parent
def foo
"Child#foo"
@sionide21
sionide21 / python.py
Created July 11, 2014 14:42
Complex default parameters: Ruby vs Python
def foo(a=[]):
a.append(1)
return a
foo() # => [1]
foo() # => [1, 1]
foo() # => [1, 1, 1]
{
"java": {
"jdk_version": "7",
"oracle": {"accept_oracle_download_terms": "true"},
"accept_license_agreement": "true",
"install_flavor": "oracle"
},
"elasticsearch": {
"nginx": {
"users": [{"username": "salesloft", "password": "<haha you wish>"}],
@sionide21
sionide21 / sqrtSums.hs
Created December 6, 2013 13:52
sqrtSums | sum of the square root of the first natural numbers sqrtSums < x
sqrtSums :: Double -> Int
sqrtSums x = length (takeWhile (<x) (scanl1 (+) (map sqrt [1..]))) + 1
@sionide21
sionide21 / gist:7807702
Last active December 30, 2015 09:09
This `ls` took over 3 hours to complete. I have too many files.
$ time ls -lh > ~/files.txt
real 186m14.030s
user 0m9.361s
sys 43m37.939s
ve() {
local curdir ve
curdir="$PWD"
[ -z "$1" ] && ve="ve" || ve="$1"
while ! ls -d "$ve" >/dev/null 2>&1; do
if [ "$PWD" == "/" ]; then
echo "Cannot find a '$ve' directory on the current path" >&2
cd $curdir
return -1
fi
@sionide21
sionide21 / gist:5214764
Created March 21, 2013 17:13
PEP8 and pyflakes aliases for git
[alias]
pep8 = !git status -s | awk '{print $2}' | grep '.py' | xargs pep8
pyflakes = !git status -s | awk '{print $2}' | grep '.py' | xargs pyflakes
@sionide21
sionide21 / regexen.rb
Created July 31, 2012 00:59
Regex game regression tester
class Regexen
def initialize(a={})
a.default_proc = proc { |h,k| [] }
@good = a[:good]
@bad = a[:bad]
end
def good=(r)
@good << r
end
def bad=(r)
@sionide21
sionide21 / taxes.rb
Created June 30, 2012 15:00
Taxes Estimator 2012
SINGLE_RATES = {8700 => 0.10, 35350 => 0.15, 85650 => 0.25, 178650 => 0.28, 388350 => 0.33, Float::INFINITY => 0.35}
MARRIED_RATES = {17400 => 0.10, 70700 => 0.15, 142700 => 0.25, 217450 => 0.28, 388350 => 0.33, Float::INFINITY => 0.35}
def taxes(amt, rates=SINGLE_RATES)
last_cap = 0
taxes = 0
rates.each do |cap, rate|
taxable = amt - last_cap
if taxable > 0
taxes += [cap, taxable].min * rate