This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
number_of_rolls = 100 | |
stats = Hash.new {0} | |
rolls = [] | |
outcome_translation = { | |
2 => "two", | |
3 => "three", | |
4 => "four", | |
5 => "five", | |
6 => "six", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ bash ./test.sh | |
bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
OlderNewer