Skip to content

Instantly share code, notes, and snippets.

class Array
# If you want the first n items from an array, you could call first(n)
# but if the array has less than n members, so will the return value.
# This method ensures that the return value has as many items as requested,
# by cycling through the array from the beginning.
# e.g. [1,2,3].modulo(2) => [1,2]
# [1,2,3].modulo(4) => [1,2,3,1]
# [1,2,3].modulo(7) => [1,2,3,1,2,3,1]
def modulo(required_size)
required_size = size if required_size.nil?
class Array
# Handy when outputing a list in two columns, such as:
#
# A B A C A C A D
# B B D B E
# C
#
def columnize
if size <= 1
[ self, [] ]
@nelstrom
nelstrom / gist:219168
Created October 27, 2009 00:19
tabsize.vim
" Set tabstop, softtabstop and shiftwidth to the same value
command! -nargs=* Stab call Stab()
function! Stab()
let l:tabstop = 1 * input('set tabstop = softtabstop = shiftwidth = ')
if l:tabstop > 0
let &l:sts = l:tabstop
let &l:ts = l:tabstop
let &l:sw = l:tabstop
endif
call SummarizeTabs()
" Switch wrap off for everything
set nowrap
if has("autocmd")
" This is probably in your .vimrc already. No need to duplicate!
filetype plugin indent on
" Set File type to 'text' for files ending in .txt
autocmd BufNewFile,BufRead *.txt setfiletype text
if has("autocmd")
" This is probably in your .vimrc already. No need to duplicate!
filetype plugin indent on
" When editing a file, always jump to the last known cursor position.
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
endif
# Giles Bowkett, Greg Brown, and several audience members from Giles' Ruby East presentation.
# http://gilesbowkett.blogspot.com/2007/10/use-vi-or-any-text-editor-from-within.html
require 'tempfile'
module Exec
extend self
def system(file, *args)
Kernel::system(file, *args)
end
" Delete all inactive buffers by running:
" :call CloseHiddenBuffers()
" or the
" :Only
command! -nargs=* Only call CloseHiddenBuffers()
function! CloseHiddenBuffers()
" figure out which buffers are visible in any tab
let visible = {}
for t in range(1, tabpagenr('$'))
for b in tabpagebuflist(t)
@nelstrom
nelstrom / XTemplate.js
Created October 18, 2010 18:18
Creating an XTemplate from an array of strings.
Ext.setup({
onReady: function() {
var data = [{
name: 'Joshua',
age:3
},{
name: 'Matthew',
age:2
},{
@nelstrom
nelstrom / XTemplate.from.js
Created October 18, 2010 18:20
Creating an XTemplate from the innerHTML of a DOM element.
Ext.setup({
onReady: function() {
var data = [{
name: 'Joshua',
age:3
},{
name: 'Matthew',
age:2
},{
@nelstrom
nelstrom / index.html
Created October 19, 2010 10:11
Creating an XTemplate from the innerHTML of a <textarea> element.
<!DOCTYPE html>
<html>
<head>...[load sencha js/css etc.]...</head>
<body>
<textarea id="kids-list" class="x-hidden-display">
<p>Kids:</p>
<tpl for=".">
<p>{#}. {name}</p>
</tpl>