Skip to content

Instantly share code, notes, and snippets.

@SteveBenner
SteveBenner / cap-execute_at.rb
Created November 6, 2013 09:25
Capistrano DSL addition for executing a command under a specific working directory
# allows one to specify the working directory for command execution
def execute_at(path, cmd)
execute "cd #{path} && #{cmd}"
end
@SteveBenner
SteveBenner / subdv.rb
Last active December 17, 2015 21:39
Subdivide a list into sub-lists of specified length
# @param [Array<Integer>] List of sizes of the partitions self will be subdivided into
# @return [Object] Array containing self divided into one or more sub-arrays of size n
# @example
# [1,2,3].subdv [1] #=> [[1]]
# [1,2,3].subdv [3] #=> [[1,2,3]]
# [1,2,3].subdv [1,2] #=> [[1],[2,3]]
# [1,2,3].subdv [1,2] #=> [[1],[2,3]]
# [1,2,3].subdv [1,1,1] #=> [[1],[2],[3]]
# [1,2,3].subdv [1,2,1] #=> [[1],[2,3],[]]
def subdv(arr)
@SteveBenner
SteveBenner / halves.rb
Last active December 17, 2015 21:39
Divide an array into equal halves
# @return [Array] Two-member Array containing self evenly split into two sub-arrays
# @example
# [1,2,3,4].halves #=> [[1,2],[3,4]]
# [1,2,3].halves #=> [[1,2],[3]]
def halves
[self[0..(self.size/2.0).round-1], self[(self.size/2.0).round..self.size]]
end
@SteveBenner
SteveBenner / zipr.rb
Last active December 17, 2015 21:39
Recursive zip
# @param [Integer] n Number of times to call zip upon self
# @return [#zip] Self, after calling Array#zip upon self given number of times
def zipr(n = self.length)
r = self
n.times { r = r.zip }
r
end
@SteveBenner
SteveBenner / htmlElem.js
Last active December 17, 2015 01:29
This function constructs an HTML element and returns it, wrapped in a jQuery object
/**
* Constructs and returns an HTML element wrapped in a jQuery object
*
* @param {String} tag Name of the HTML element
* @param {Object} attr Map of attribute key/value pairs for the HTML element
* @param {String} content Content to be directly inserted inside HTML tags
*/
function htmlElem(tag, attr, content) {
tag = tag || 'div'; attr = attr || ''; content = content || ''; // optional parameters
var htmlString = '<' + tag
@SteveBenner
SteveBenner / _color-palette.slim
Created October 29, 2015 14:32
Slim partial - renders a color palette on your web page to aid in graphic design
/ This is a humble web design tool inspired by the eyedropper functionality in Google Chrome's
color picker interface (part of the dev tools panel) which allows one to choose any pixel of
the current web page to set as a color property's value. Though hugely convenient for visual
design, color selection being limited to the context of the page leaves much to be desired.
/ This partial solves the limitation by affixing an unobtrusive panel to your page to act as
a makeshift color palette to use in conjunction with the eyedropper. The panel is populated
with small squares representing a list of colors passed into the partial via local. Viola!
/ @local [Array<String>] colors List of values valid for `background-color` CSS property
@SteveBenner
SteveBenner / gh-starred.rb
Created March 15, 2015 09:46
Produce a Markdown file enumerating the starred repos of a GitHub user (with HTML links) using the Unicorn API gem
# The 'github_api' gem is implicility assumed to have been loaded at this point...
# Configuration data is passed to Github::Client#new as a Hash whose keys are Symbols
CONFIG_DATA = {
user: 'github-username',
login: 'github-login-email@host.com',
oauth_token: 'personal-access-token' # one of many valid authentication methods (see the gem docs for more)
}
# Data is returned from the API as a Github::ResponseWrapper object
@SteveBenner
SteveBenner / regexes.md
Last active August 29, 2015 14:09
Useful Regular Expressions I’ve come up with

Regular Expressions

Text coloring / highlighting

I use the following regexes with [iTerm2][iterm]'s ['trigger' feature][it1] to create some basic syntax highlighting.

(?<=- )([A-Z][A-Z ]+:)+ Colorize CONSTANTS Magenta
(?<=- )(:|")\w*"?(?= =>) Colorize :keys => / "keys" => Yellow
^([^=]*)(?==) Colorize ENV_VARS Cyan
(?&lt;=[^=])= Colorize the = separator Dark Grey

body.loading:after {
/* with no content, nothing is rendered */
content: "";
position: fixed;
/* element stretched to cover during rotation an aspect ratio up to 1/10 */
top: -500%;
left: -500%;
right: -500%;
bottom: -500%;
z-index: 9999;
@SteveBenner
SteveBenner / _google-analytics.slim
Created September 26, 2014 05:43
Slim partial - Google analytics
/ Official Google Analytics tracking code (updated 2014-10-23)
/
/ @option [String] google_analytics_key GA key in a format similar to: 'UA-XXXXXXXX-X'
/
- if google_analytics_key
javascript:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');