Skip to content

Instantly share code, notes, and snippets.

@sshaw
sshaw / convert-phone-number.rb
Last active February 26, 2017 02:53
Convert a Vanity Phone Number With Letters to One With Numbers Only
# https://gist.github.com/sshaw/29d6f7379771e3b4596e228b626bcf9a
def convert(chr)
chr = chr.upcase
# subtract "A"
n = (chr.ord - 65) / 3
# account for #7 & #9 which have 4 chars
n -= 1 if chr == "S".freeze || chr == "V".freeze || chr >= "Y".freeze
(n + 2).to_s
end
@sshaw
sshaw / template.rb
Created November 17, 2016 04:16
Class for rendering ERB templates using "local variable style" in templates
require "erb"
class Template
def initialize(template)
@__template = ::ERB.new(template, nil, "-")
end
def render(__vars = {})
__b = binding
__names = []
@sshaw
sshaw / to_pluralizer.rb
Last active September 10, 2016 23:37
String#to_pluralizer: Create a pluralization Proc from a String
require "active_support/core_ext/string/inflections"
class String
def to_pluralizer
->(*a) { pluralize(*a) }
end
end
__END__
CUSTOMER = "customer".to_pluralizer
@sshaw
sshaw / iterm2_image.rb
Last active July 28, 2016 20:48
Ruby class & program to take an image and output it using an iTerm2 escape sequence. Supports all of the iTerm2 image protocol.
require "base64"
#
# Take an image and output it using an iTerm2 escape sequence.
# Supports all of the iTerm2 image protocol: https://iterm2.com/documentation-images.html
#
# https://gist.github.com/sshaw/3687e4acdc2e1e9080d3dff04474e4e5
module ITerm2
class Image
@sshaw
sshaw / refills_flash.rb
Last active July 27, 2016 23:17
Ruby helper to render Bourbon/Neat Refills style flash messages
# Helper to render Bourbon/Neat Refills style flash messages
# https://gist.github.com/sshaw/bc27941c20f0adbfa5b5c9c57da095d7
module RefillsFlash
FLASH_KEYS = [:success, :notice, :error, :alert]
def flash_messages
FLASH_KEYS.inject("") do |html, name|
if flash[name]
html << content_tag(:div, :class => "flash-#{name}") {
content_tag :span, flash[name]
@sshaw
sshaw / query_source.rb
Last active June 27, 2016 01:14
Make debugging a bit easier by determining the source of ActiveRecord queries. The source of queries will be output via logger.debug().
#
# Make debugging a bit easier by determining the source of ActiveRecord queries.
# The source of queries will be output via logger.debug().
#
# By: Skye Shaw (https://github.com/sshaw)
# Date: 2014-10-24
# Source: https://gist.github.com/sshaw/05d8bdf99e4175816db858323fb2169c
#
# === Usage
#
@sshaw
sshaw / request-bin.el
Last active April 30, 2016 04:33
Interactive Emacs function to create a RequestBin on http://requestb.in/
;;; request-bin.el --- Create a RequestBin on http://requestb.in/
;; Author: Skye Shaw <skye.shaw@gmail.com>
;; Version: 0.0.1
;; Keywords: tools
;; URL: https://gist.github.com/19d1e0d85eadd555c178ae1754cc6211
;; This file is NOT part of GNU Emacs.
;;; License:
@sshaw
sshaw / gh_update_remotes_owner.sh
Last active March 21, 2016 15:58
Batch update the GitHub user or organization in all repository remotes under the given directory.
# Batch update the GitHub user or organization in all repo remotes under the given directory.
# Does not work with JRuby.
#
# usage: usage: gh_update_remotes_owner dir oldorg neworg
gh_update_remotes_owner()
{
if [ $# -lt 3 ]; then
echo "usage: gh_update_remotes_owner dir oldorg neworg" >&2
return 1
fi
@sshaw
sshaw / gist:1190047
Last active March 20, 2016 03:03
Ruby String class extensions for checking file permissions
class String
MODES={"r" => :readable?, "w" => :writable?, "x" => :executable?}
def permission?(file)
self.split(//).inject(true) { |can, m| can && MODES.include?(m) && File.send(MODES[m], file) }
end
end
"rw".permission?("Makefile.PL")
@sshaw
sshaw / gist:1342663
Last active March 20, 2016 03:02
Ruby Pathname extensions to check for parent and subdirectory: Pathname.under? and Pathanme.above?
class Pathname
def under?(path)
parent, child = [ Pathname.new(path), self ].inject([]) { |a, p| a << p.expand_path.to_s.split(File::SEPARATOR) }
parent.drop_while { |p| p == child.shift }.none? && child.any?
end
def above?(path)
Pathname.new(path).under? self
end
end