Skip to content

Instantly share code, notes, and snippets.

The book is devoted to metaprogramming. To be brief, metaprogramming is writing code that writes code.
Book is divided to two parts: big and small. The big part is devoted to metaprogramming in Ruby directly.
But the small little touches metaprogramming in Ruby on Rails. Let's consider the big (and the most
important) part. It consists of 5 parts, title as work days of week. When reading the book it assumes,
that the reader learns something new every day and mastering metaprogramming in five days. Of course,
it's not possible to become a professional in 5 days, or 24 hours or some another short term, Yet the
style of narration does not detract from the merits of the book, but vice versa. They support direct
dialogue with the reader, proposing to solve some of tasks themselves. The book examines specific examples.
But truly say, only in the fifth chapter the author refers directly to the heart of metaprogramming. In the
first four chapters he trains the reader to the Ruby conception.
@schipiga
schipiga / gist:5479741
Last active December 16, 2015 18:39
Automatically start python virtual environment (better variant) - put this chunk of code to .bashrc or .bash_profile; - be sure that virtual environment is present in your local git repository; - add .env/* to .gitignore
workon_virtualenv() {
if [[ $(git status 2>/dev/null) ]] || [[ $(git status 2>/dev/null) ]]
then
deactivate >/dev/null 2>&1
$(cat $(hg root 2>/dev/null)/.pvmrc) 1>/dev/null || $(cat $(git rev-parse --show-toplevel 2>/dev/null)/.pvmrc) 1>/dev/null
# source $(hg root 2>/dev/null)/.env/bin/activate 2>/dev/null || source $(git rev-parse --show-toplevel 2>/dev/null)/.env/bin/activate 2>/dev/null
else
deactivate >/dev/null 2>&1
fi
}
@schipiga
schipiga / gist:5562506
Last active December 17, 2015 05:59
Bash git branch
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(git::\1)/'
}
parse_svn_branch() {
parse_svn_url | sed -e 's#^'"$(parse_svn_repository_root)"'##g' | awk -F / '{print "(svn::"$1 "/" $2 ")"}'
}
parse_svn_url() {
svn info 2>/dev/null | grep -e '^URL*' | sed -e 's#^URL: *\(.*\)#\1#g '
}
parse_svn_repository_root() {
@schipiga
schipiga / gist:5629255
Last active December 17, 2015 15:09
~/.hgrc
[extensions]
color =
pager =
[color]
status.modified = cyan bold
status.added = green bold
status.removed = red bold
status.deleted = red bold
status.unknown = magenta bold
@schipiga
schipiga / gist:6146192
Created August 3, 2013 11:49
custom tee
def tee(iterable, n=2):
try:
iter(iterable)
except TypeError:
raise Exception("%s isn't iterable, silly boy :)" % iterable)
def f(iterable):
for i in iterable:
yield i
return [f(iterable) for i in xrange(n)]
@schipiga
schipiga / gist:6146344
Last active December 20, 2015 14:28
custom peekable
def peekable(generator):
import types
if not isinstance(generator, types.GeneratorType):
raise Exception("%s isn't generator, silly girl ;)" % generator)
def stop_iterator(func):
def wrapper(*args, **kwgs):
try:
return func(*args, **kwgs)
@schipiga
schipiga / gist:d037afd2255d3588a543
Last active August 29, 2015 14:04
python subscriber pattern
from threading import Thread
class Subscriber(object):
_instance = None
def __new__(cls):
if not cls._instance:
cls._instance = super(Subscriber, cls).__new__(cls)
cls._instance.init()
@schipiga
schipiga / gist:6500e54f4335fc071c11
Last active August 29, 2015 14:04
python TaskQueuer with simple interface
import json
from celery import Celery
try:
import cPickle as pickle
except ImportError:
import pickle
We couldn’t find that file to show.
@schipiga
schipiga / gist:11b137ea07aa2316f39b
Last active August 29, 2015 14:18
run function once only
def run_once(func):
setattr(func, 'was_launched', False)
def wrapper(*args, **kwgs):
if getattr(func, 'was_launched'):
return
setattr(func, 'was_launched', True)
return func(*args, **kwgs)
return wrapper