Skip to content

Instantly share code, notes, and snippets.

@legkvla
legkvla / gist:9684622
Created March 21, 2014 11:57
Binding example
require 'erb'
class Namespace
def initialize(hash)
hash.each do |key, value|
singleton_class.send(:define_method, key) { value }
end
end
def get_binding
@legkvla
legkvla / playground.rb
Created April 10, 2014 11:44
Textile postprocessing using gsub
before = "h1. Ruby\nHello world"
puts "Before: #{before}"
res = before.gsub(/h1\..+\n/) {|c| "\n#{c}\n" }
puts "After: #{res}"
@legkvla
legkvla / playground2.rb
Created April 10, 2014 11:59
Fixing textile - end of bullets
before = "* Ruby\nHello world"
puts "Before: #{before}"
res = before.gsub(/\*.+\n+\s*[^\*]/) {|c| c.gsub(/\n+\s*/, "\n\n") }
puts "After: #{res}"
@legkvla
legkvla / birhdays_query.sql
Last active August 29, 2015 14:00
Birthday ordering on postgres
select * from
(
select
CASE
WHEN days < (current_date - date_trunc('year', current_date)) THEN days + interval '366 days'
ELSE days
END as position, birthday
from
(select birthday, birthday - date_trunc('year', birthday) as days from birthdays) bd_in_days
) bd
@legkvla
legkvla / convert_dicts
Created July 25, 2014 07:58
Russian full text search setup in pgsql
#path can be different. For Red Hat it is /opt/PostgreSQL/9.3/share/postgresql/tsearch_data
iconv -f koi8-r -t utf-8 < ru_RU.aff > /usr/share/postgresql/tsearch_data/russian.affix
iconv -f koi8-r -t utf-8 < ru_RU.dic > /usr/share/postgresql/tsearch_data/russian.dict
@legkvla
legkvla / profiler.rb
Created August 28, 2014 08:28
Simple Ruby Profiler for JRuby
require 'java'
class Profiler
def initialize(name)
@name = name
@metrics = {}
@start_times = {}
end
@legkvla
legkvla / time_on_rows
Last active August 29, 2015 14:06
My Mondrian DB query examples
SELECT
non empty {[Product].[All Products].Children} ON COLUMNS,
non empty {[Time].[Quarter].members, [Time].[Year].members} ON ROWS
FROM [Sales]
@legkvla
legkvla / wc.py
Created March 29, 2015 20:32
WC replacement on python (I was needed it for my busy-box)
#!/usr/bin/env python
import sys
if __name__ == "__main__":
# Initialize a names dictionary as empty to start with.
# Each key in this dictionary will be a name and the value
# will be the number of times that name appears.
names = {}
# sys.stdin is a file object. All the same functions that
# can be applied to a file object can be applied to sys.stdin.
@legkvla
legkvla / SimpleLock.java
Last active August 29, 2015 14:19
Jgroups lock example (warning: in normal code you must unlock in finally block)
package ru.examples;
import org.jgroups.JChannel;
import org.jgroups.blocks.locking.LockService;
import java.util.concurrent.locks.Lock;
public class SimpleLock {
/** You should provide one parameter - node name */
mike@rbci:~$ psql -U postgres
psql (9.0.3)
Type "help" for help.
postgres=# update pg_database set datallowconn = TRUE where datname = 'template0';
UPDATE 1
postgres=# \c template0
You are now connected to database "template0".
template0=# update pg_database set datistemplate = FALSE where datname = 'template1';
UPDATE 1