Skip to content

Instantly share code, notes, and snippets.

View aks's full-sized avatar
💭
Working

Alan Stebbens aks

💭
Working
View GitHub Profile
@aks
aks / mv-zeroes.rb
Created July 6, 2015 17:30
Move Zeroes -- Amazon Quesion
#!/usr/bin/env ruby
# mv-zeroes
#
# Beware! Today's exercise, which derives from an interview question asked at
# Facebook, is trickier than it looks:
#
# You are given an array of integers. Write a program that moves all non-zero
# integers to the left end of the array, and all zeroes to the right end of the
# array. Your program should operate in place. The order of the non-zero
# integers doesn't matter. As an example, given the input array
@aks
aks / db.rake
Last active December 11, 2015 03:03 — forked from hopsoft/db.rake
Rails rake tasks for dump & restore of PostgreSQL databases
# lib/tasks/db.rake
namespace :db do
desc "Dumps the database to backups"
task :dump => :environment do
dump_fmt = 'c' # or 'p', 't', 'd'
dump_sfx = suffix_for_format dump_fmt
backup_dir = backup_directory true
cmd = nil
@aks
aks / keybase.md
Created March 14, 2017 22:01
keybase.md

Keybase proof

I hereby claim:

  • I am aks on github.
  • I am aks (https://keybase.io/aks) on keybase.
  • I have a public key ASCXHS3cbDV5-Um2mX-N5C1IeAz7NCPBbyW4JhcqcXXhrwo

To claim this, I am signing this object:

@aks
aks / Main.elm
Last active April 28, 2017 16:35 — forked from anonymous/Main.elm
SVG Clock with hands
-- Read more about this program in the official Elm guide:
-- https://guide.elm-lang.org/architecture/effects/time.html
module Main exposing (..)
import Html exposing (Html, div, br)
import String exposing (padLeft, join)
import Date
import Color exposing (Color)
@aks
aks / install-pre-push-hook.sh
Created July 20, 2017 17:16
Bash script to install a `pre-push-hook` into the current git repo
#!/usr/bin/env bash
# install-pre-push-hook
# this script installs the pre-push hook into the current .git repo.
# by default, the pre-push hook protects the "master" branch.
set_git_hooks_dir_path() {
set_git_dir_path
git_hooks_dir_path="$git_dir_path/hooks"
if [[ ! -d "$git_hooks_dir_path" ]] ; then
echo 1>&2 "$git_hooks_dir_path does not exist!"
exit
@aks
aks / rails_env_tester.rb
Last active September 11, 2017 15:56
Show independence of Rails.env and RAILS_ENV
#!/usr/bin/env ruby
#
def show_env(msg=nil)
puts msg if msg
puts "Rails.env = #{Rails.env}"
puts "RAILS_ENV = #{ENV['RAILS_ENV']}"
puts ''
end
@aks
aks / threaded-class-variable-usage-test-script.txt
Last active October 30, 2017 18:27
Demonstrate class variable sharing across threads
$ ruby threaded-class-variable-usage-test.rb
#<Thread:0x007f8a329d6ea8>: New state = #<Thread:0x007f8a329d6ea8>
#<Thread:0x007f8a329d6fe8>: New state = #<Thread:0x007f8a329d6fe8>
#<Thread:0x007f8a329d7240>: New state = #<Thread:0x007f8a329d7240>
#<Thread:0x007f8a329d7128>: New state = #<Thread:0x007f8a329d7128>
#<Thread:0x007f8a329d5eb8>: New state = #<Thread:0x007f8a329d5eb8>
#<Thread:0x007f8a329d6fe8>: State changed! my_name = #<Thread:0x007f8a329d6fe8>; Foo.state = #<Thread:0x007f8a329d5eb8> ; checks = 1
#<Thread:0x007f8a329d7240>: State changed! my_name = #<Thread:0x007f8a329d7240>; Foo.state = #<Thread:0x007f8a329d5eb8> ; checks = 1
#<Thread:0x007f8a329d7128>: State changed! my_name = #<Thread:0x007f8a329d7128>; Foo.state = #<Thread:0x007f8a329d5eb8> ; checks = 1
#<Thread:0x007f8a329d6ea8>: State changed! my_name = #<Thread:0x007f8a329d6ea8>; Foo.state = #<Thread:0x007f8a329d5eb8> ; checks = 2
@aks
aks / match_vs_include_rspec_test.rb
Created January 5, 2018 19:58
Measure `match` vs `include` in rspecs on string tests
require 'rails_helper'
require 'benchmark'
RSpec.describe 'testing match vs includes' do
let(:subject) { "This is the time for all good men to come to the aid of their country" }
HOW_MANY_TIMES = 100_000
Benchmark.bm(10) do |bm|
bm.report("match:") do
@aks
aks / test_match_spec.rb
Created January 28, 2018 01:36
Benchmarking rspec matchers "match" and "include"
require 'rails_helper'
require 'benchmark'
RSpec.describe 'testing match vs includes' do
let(:subject) { "This is the time for all good men to come to the aid of their country" }
HOW_MANY_TIMES = 100_000
Benchmark.bm(10) do |bm|
bm.report("match:") do
@aks
aks / capture_output.rb
Created February 5, 2018 23:29
Spec helper class: CaptureOutput.run { block }
class CaptureOutput
# Use in test examples to run a command *once*, and then
# have the stdout and/or stderr available for testing
#
# out = CaptureOutput.run do
# run_some_command
# end
#
# expect(out.stdout).to match(/some output/)