Skip to content

Instantly share code, notes, and snippets.

View achempion's full-sized avatar
✍️
Writing software

Boris Kuznetsov achempion

✍️
Writing software
View GitHub Profile
main :: IO ()
main = do
str <- getLine
let [a,b] = words str
return ((read a :: Int) + (read b :: Int))
@achempion
achempion / monkey-patching.rb
Created January 27, 2015 08:43
православный манкипатчинг
module MyStringThing
refine String do
def at(num)
self[num..num]
end
end
end
class A
module Enumerable
class Lazy < Enumerator
def initialize(obj)
super() do |yielder|
obj.each do |val|
if block_given?
yield(yielder, val)
else
yielder << val
#!/usr/bin/env bash
# This script prints out all of your Redis keys and their size in a human readable format
# Copyright 2013 Brent O'Connor
# License: http://www.apache.org/licenses/LICENSE-2.0
human_size() {
awk -v sum="$1" ' BEGIN {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; } '
}
@achempion
achempion / content_tag.js
Created October 14, 2012 14:15
Простой метод, позволяет создавать теги как с помощью рельсового метода `content_tag`
var content_tag;
content_tag = function(tag, content, params){
var tag, content, params, res;
var insert_params = '';
for(var key in params){
insert_params += ' '+key+'='+'"'+params[key]+'"';
}
if(content){
res = '<'+tag+insert_params+'>'+content+'</'+tag+'>';
} else {
@achempion
achempion / internalization_helper.rb
Created October 22, 2012 20:00
simple db localization
module InternalizationHelper
# let's imagine that some methods defined in your model like 'title_en' and 'title_ru' for different locales
# helper will create 'title' method depending on current I18n.locale
# Model.rb
# extend InternalizationHelper
# define_translations_for 'title', 'description', ...
def define_translations_for *methods
methods.each do |method_name|
@achempion
achempion / i18n_builder_method.rb
Last active December 14, 2015 14:38
i18n builder method
def set_contents_builder model_obj
model_name = model_obj.class.name.downcase
accociation = model_obj.send("#{model_name}_contents").group_by {|content| content.lang}
if accociation['ru'].nil?
model_obj.send("#{model_name}_contents").build(lang: 'ru')
end
if accociation['en'].nil?
model_obj.send("#{model_name}_contents").build(lang: 'en')
end
end
@achempion
achempion / gist:c0bdb0a173bd106de955
Created February 16, 2016 21:26
pfctl cheat sheet
# basic pfctl control
# ==
# Related: http://www.OpenBSD.org
# Last update: Tue Dec 28, 2004
# ==
# Note:
# this document is only provided as a basic overview
# for some common pfctl commands and is by no means
# a replacement for the pfctl and pf manual pages.
$ ->
$(document).on 'ajax:error', '.js-form-validation', (e, xhr, status, error) ->
$form = $(@)
resource_name = $form.attr('data-js-form-validation-resource')
$form.find('.field_with_errors').removeClass('field_with_errors')
$form.find('.error-message').removeClass('is_showed').text('')
setTimeout ->
$.each xhr.responseJSON, (key, value) ->
alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias gco='git checkout '
alias gp='git push '
alias gpf='git push --force '
alias gpr='git pull --rebase '