Skip to content

Instantly share code, notes, and snippets.

View JohnB's full-sized avatar

John Baylor JohnB

View GitHub Profile
@JohnB
JohnB / lun_check.rb
Created August 16, 2012 06:38
Ruby LUN checker
# each_with_index is useful, so why not inject_with_index?
module Enumerable
def inject_with_index(memo)
each_with_index {|obj, idx| memo = yield(memo, obj, idx)}
memo
end
end
def passes_lun?(cardnum)
digits = cardnum.gsub(/\D/,'').reverse.split('').map(&:to_i)
@JohnB
JohnB / array_extensions.rb
Last active December 14, 2015 15:48
Array.extract
# We need to create Array.extract if it doesn't already exist
class Array
# Delete some of our items and return (or "extract") a new array holding the deleted items.
# Very similar to partition, but instead of returning copies, it modifies the original list
def extract
extracted = []
# reverse our deletion so indexes don't get messed up while we're deleting
(length - 1).downto(0) do |idx|
item = self[idx]
extracted << delete_at(idx) if yield(item)
@JohnB
JohnB / vj.sh
Last active May 25, 2020 19:29 — forked from kljensen/vj.sh
Added location, updated link to the access-os-x-keychain-from-terminal blog post and added some explanatory text.
#!/usr/bin/env bash
# Canonical location: https://gist.github.com/kljensen/4434992
##########################################################
#
# This is a shell script for keeping a journal that is
# * plaintext,
# * time-stamped,
# * encrypted, and
@JohnB
JohnB / collaborators.rb
Last active December 26, 2015 00:48
List your repositories and their collaborators
require 'json'
#
# List your repositories and their collaborators
#
# Before running this script, follow these steps:
# 1) get an OAuth token from https://github.com/settings/applications
# 2) add it to your environment with "export GW=abc123..."
# 3) add your github user to your environment (e.g. "export GWUSER=JohnB" is mine)
# 4) run this script via "ruby collabotators.rb"
#
@JohnB
JohnB / loginButtons
Created April 1, 2014 22:03
How meteor's {{loginButtons}} worked until October 2013
// NOTE: this code was replaced here:
// https://github.com/meteor/meteor/commit/119682c8ec26e84e63e3e62bf0510d515aa78d97#diff-e7d59a612c271f7d2ee876d7802cf170L4
Handlebars.registerHelper( // 4
"loginButtons", // 5
function (options) { // 6
if (options.hash.align === "right") // 7
return new Handlebars.SafeString(Template._loginButtons({align: "right"})); // 8
else // 9
return new Handlebars.SafeString(Template._loginButtons({align: "left"})); // 10
@JohnB
JohnB / list_comprehension.rb
Created November 20, 2014 19:54
List comprehension in ruby
# Inspired by https://blog.engineyard.com/2014/ruby-list-comprehension
module Kernel
def c(comprehension, &block)
scope = block.send(:binding)
if comprehension =~ /^\s*(\S.*)\s+for\s+(\S+)\s+in\s+(.*)$/
amount, for_variable, in_range = $1,$2,$3
scope.send(:eval, in_range).map do |value|
scope.send(:eval, "#{for_variable} = #{value}")
scope.send(:eval, "#{for_variable} = #{amount}")
@JohnB
JohnB / stack.ex
Last active October 22, 2017 20:32
Object-oriented version of the canonical stack example.
defmodule Stack do
use GenServer
# Public Interface (runs in the caller's process)
def create() do
{:ok, pid} = GenServer.start_link(__MODULE__, [])
pid
end
@JohnB
JohnB / gist:42706e097544f2a7f1f31209dcdef40d
Created August 17, 2020 18:20
Phoenix dir, built by John Baylor, for a user queue JSON API for Tubi.
gist --login

Keybase proof

I hereby claim:

  • I am johnb on github.
  • I am johnb (https://keybase.io/johnb) on keybase.
  • I have a public key ASB5S-uxyB8B6V4NPLsrhSuT5YiyajgCUXwWVDoQ_eHGPAo

To claim this, I am signing this object:

@JohnB
JohnB / y_axis.ex
Last active July 20, 2022 20:20
Example code to improve the MapMyWalk altitude graph
defmodule YAxis do
@moduledoc """
The YAxis module attempts to find "reasonable" values for the MapMyWalk
altitude graph. For example, my MapMyWalk graph (as of 7/2022) offers
these altitude values when my workout goes from 138 to 198 feet:
20.4, 79.5, 138.5, 197.6, 256.6, 315.7
Which I find to be much less readable/understandable than:
120, 140, 160, 180, 200, 220