Skip to content

Instantly share code, notes, and snippets.

View skanev's full-sized avatar

Stefan Kanev skanev

View GitHub Profile
@skanev
skanev / Makefile
Last active August 29, 2015 14:01
Code from a live demo on the Python FMI course
gui:
python2 gui.py
cli:
python3 cli.py
test:
python3 tests.py
#!/usr/bin/env zsh
c_dir=~/.zsh/contexts
c_contexts=($c_dir/*(:t))
c-chpwd() {
local matches
matches=(${${(s:/:)PWD}:*c_contexts})
if [[ $#matches -gt 1 ]]; then
@skanev
skanev / counter_cache_test.rb
Last active August 29, 2015 14:05
An illustration of counter_cache and optimistic locking incompatiblity
require 'active_record'
require 'minitest/autorun'
require 'sqlite3'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection 'sqlite3::memory:'
ActiveRecord::Schema.define do
create_table :people do |t|
@skanev
skanev / functional_programming.rb
Last active August 29, 2015 14:14
My "slides" from WCEU 2014
# encoding: utf-8
def annotate(string, code = '4')
string.gsub(/@(.*?)@/, "\e[#{code}m\\1\e[0m")
end
def colorize(string)
string.gsub(/#(.*?)#/, "\e[\\1m").gsub(/##/, "\e[0m")
end
@skanev
skanev / MATH_JOKE.markdown
Created January 29, 2015 14:08
Best. Math Joke. Ever.

When the Ark eventually lands after the Flood, Noah releases all the animals and makes a proclamation: "Go forth and multiply".

Several months later, Noah is delighted to see that all the creatures are breeding, except a pair of snakes, who remain childless. Noah asks: "What's the problem?" The snakes have a simple request of Noah: "Please cut down some trees and let us live there."

Noah obliges, leaves them alone for a few weeks and then returns. Sure enough, there are lot of baby snakes. Noah asks why it was important to cut down the trees, and the snakes reply: "We're adders, and we need logs to multiply."

# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
require 'spec/autorun'
require 'spec/rails'
Dir[File.dirname(__FILE__) + "/spec_helpers/**/*.rb"].each do |file|
require file
end
aquarius@arrakeen ~ $ cat .screenrc | tail -n 19 | head -n 6
# Make shift-PgUp and shift-PgDn work like they do in xterm. (Note that this
# requires xterm to be configured to pass those keys through, and not try to
# act on them itself.)
bindkey "^[[5;2~" eval "copy" "stuff ^u"
bindkey -m "^[[5;2~" stuff ^u
bindkey -m "^[[6;2~" stuff ^d
def egn_valid?
egn =~ /^(\d{2})(\d{2})(\d{2})\d{3}(\d)$/ and
egn.each_char[0...9].zip([2, 4, 8, 5, 10, 9, 7, 3, 6]).sum { |d, w| d.to_i * w } % 11 % 10 == $4.to_i and
Date.valid_civil?($1.to_i, $2.to_i % 20, $3.to_i) and
$2.to_i / 20 <= 2
end
# Алгоритъм от тук: http://www.grao.bg/esgraon.html
sub opening_slide {
$_ = shift;
my ($title, $date, $intro) = (/^(.*?)\n(.*?)\n(.*)$/s);
$intro = html_escape(trim($intro));
$intro =~ s#\n#<br />\n#g;
$intro =~ s# # &nbsp;#g;
html_head($title, $date, $intro);
}
class TopicsController < ApplicationController
before_filter :set_topic, :only => [:show, :edit, :update, :destroy]
def index
@topics = current_user.topics
end
def show
@topic.update_topic_hits