Skip to content

Instantly share code, notes, and snippets.

@Frost
Frost / method_missing.py
Created June 26, 2012 13:54
method_missing in python
class MethodMissing(object):
def method_missing(self, attr, *args, **kwargs):
"""Stub: override this function"""
raise AttributeError("Missing method %s called." % attr)
def __getattr__(self, attr):
def callable(*args, **kwargs):
return self.method_missing(attr, *args, **kwargs)
return callable
@Frost
Frost / once_1.rb
Created July 18, 2012 12:25
Implementing one-shot methods in ruby
# With this method, once(:method) has to be called after the method has been added
module Once
def once(method)
method = method.to_s
class_eval <<-EVAL_END
alias_method :#{method}_once, :#{method}
def #{method}(*args)
result = send('#{method}_once', *args)
self.class.send(:remove_method, '#{method}_once')
self.class.send(:remove_method, '#{method}')
@Frost
Frost / hash_wat.rb
Created July 19, 2012 18:35
Ruby Hash wat.
# So, let's create a Hash with an empty Array as the default value...
0|frost tiamat >> irb
irb(main):001:0> h = Hash.new []
=> {}
irb(main):002:0> h[42] << "foo"
=> ["foo"]
irb(main):003:0> h
=> {}
irb(main):004:0> h[42] += ["bar"]
=> ["foo", "bar"]
@Frost
Frost / dice_average.rb
Created July 29, 2012 11:58
Made a program that rolls 100 pairs of dice and counts each result. Now in ruby.
number_of_rolls = 100
stats = Hash.new {0}
rolls = []
outcome_translation = {
2 => "two",
3 => "three",
4 => "four",
5 => "five",
6 => "six",
@Frost
Frost / run_tags.rb
Created July 31, 2012 14:32 — forked from tobias/run_tags.rb
A script for generating TAGS from a git hook.
#!/usr/bin/ruby
#-*-ruby-*-
# A script to run ctags on all .rb files in a project. Can be run on
# the current dir, called from a git callback, or install itself as a
# git post-merge and post-commit callback.
# CTAGS = '/opt/local/bin/ctags' # macports
CTAGS = '/usr/local/bin/ctags' # homebrew
HOOKS = %w{ post-merge post-commit post-checkout }
HOOKS_DIR = '.git/hooks'
@Frost
Frost / jump_to_relative_line
Created July 31, 2012 16:02
macro for jumping to a relative line in vim
" prompt user for a line to jump to.
" accepts N, +N or -N as input.
" -N will be equivalent of just typing N-
" +N equals typing N+ or running :N
function! JumpToRelativeLine()
let linenumber = line('.')
call inputsave()
let line = input('go to line: ')
call inputrestore()
call setpos('.', [0,eval(linenumber + line),0,0])
@Frost
Frost / config-deploy-staging.rb
Created August 6, 2012 08:32
Select git branch to deploy with capistrano
server 'staging.domain.tld', :app, :web, :db, :primary => true
set :branch do
default_tag = "master"
tag = Capistrano::CLI.ui.ask "What tag/branch do you want to deploy? [#{default_tag}] "
tag = default_tag if tag.empty?
tag
end
set :rails_env, 'staging'
@Frost
Frost / test run
Created August 6, 2012 14:16
set bash variables
$ bash ./test.sh
bar
class Anagrams
def self.process(word_list)
word_list.group_by {|word| word.strip.downcase.chars.sort.join }.
values.map {|v| v.map(&:strip).join(" ")}
end
end
@Frost
Frost / bigrams.rb
Created September 24, 2012 21:17
Nippes bigram
#!/usr/bin/env ruby
letters = "A".."Z"
numbers = 0..9
bigrams = letters.map do |letter|
numbers.map do |number|
letter + number.to_s
end
end