Skip to content

Instantly share code, notes, and snippets.

View adammcarth's full-sized avatar

Adam McArthur adammcarth

View GitHub Profile
string = "Hello, world!"
return string.slice(0, 5) + "..."
#=> Hello...
for (int i = 0; i < 5; i++) {
...
}
# Intelligent string truncation function
# written in Ruby.
def truncate(string, words)
# Define the maximum number of characters that can be used before
# standard truncation is applied (prevents long words from ruinning HTML).
max_chars = 6 * words
words_array = string.split(" ") # splits each word into an array ["like", "this"]
index = words - 1 # array indexes start at 0, so let's take one number off the words amount.
truncated = words_array[0..index].join(" ")
<%
# Function to shorten a string
def truncate(string, characters)
if characters < string.length
truncated = string.slice(0, characters)
return truncated + "..."
else
return string
end
function Foo( callback ) {
this.callback = callback;
}
Foo.prototype.Bar = function() {
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
console.log(this.callback);
// => undefined
// Program logic
function ShoppingList( options ) {
this.items = {};
this.defaults = options.defaults;
if ( this.defaults ) {
this.items = this.defaults;
}
}
@adammcarth
adammcarth / README.md
Created June 14, 2014 14:34
testing sublime gist generator
<script> loadCSS( href, before, media ){ ... } // load a file loadCSS( "path/to/mystylesheet.css" );
/*!
loadCSS: load a CSS file asynchronously.
[c]2014 @scottjehl, Filament Group, Inc.
Licensed MIT
*/
function loadCSS( href, before, media ){
"use strict";
// Arguments explained:
// `href` is the URL for your CSS file.
// `before` optionally defines the element we'll use as a reference for injecting our <link>
@adammcarth
adammcarth / find.rb
Last active August 29, 2015 14:02 — forked from universal/find.rb
class JTask
# JTask.find()
# Retrieves records from the database file.
# --------------------------------------------------------------------------------
# Eg: JTask.find("test.json", 1) || JTask.find("test.json", first: 10)
# #=> <JTask id=x, ...>
# --------------------------------------------------------------------------------
# See wiki guide for more usage examples...
# https://github.com/adammcarthur/jtask/wiki/find
@adammcarth
adammcarth / find.rb
Created June 11, 2014 11:53
This algorithm seems really ignorant... any suggestions?
class JTask
# JTask.find()
# Retrieves records from the database file.
# --------------------------------------------------------------------------------
# Eg: JTask.find("test.json", 1) || JTask.find("test.json", first: 10)
# #=> <JTask id=x, ...>
# --------------------------------------------------------------------------------
# See wiki guide for more usage examples...
# https://github.com/adammcarthur/jtask/wiki/find