Skip to content

Instantly share code, notes, and snippets.

@sergeych
sergeych / sorted_array.rb
Created June 21, 2010 08:28
self-sorting Array with binary search and effective support for either comparing block or item's own odrer method (<)
# +Array+ that sorts itself on the fly.
#
# * <<, +push+ and +unshift+ insert new item in the right place
# place, as well as +, += and +concat+.
#
# * +index+ and +find_index+ use extremely effective binary search.
#
# * +insert+ and +[]=+ are made private to forbid order changes.
#
# It is possible to provide comparing procedure in the block passed to the
@sergeych
sergeych / hash.rb
Created June 21, 2010 08:29
Python-inspired Hash extension: index sequence getters/setters
# Python-inspired Hash extension: index sequence getters/setters
# author:: [email protected]
#
# Example:
#
# h = { '1' => 'one', '2' => 'two', 3 => 'three'}
# h[:one, :three, :two] = h[ '1', '3', '2']
# p h # => {"1"=>"one", "2"=>"two", 3=>"three", :one=>"one", :three=>nil, :two=>"two"}
#
class Hash
@sergeych
sergeych / numcode.rb
Created March 27, 2011 18:03
The user-readable huge number encoder that corrects misreadings
# encoding: utf-8
##
# The arbitrary-length positive integers to human readable form codec (serials, links and like).
# The idea is to avoid misreading/misinterpetation of symbols. For example,
# the letter 0 o and O, ir I and 1, often looks very likely. The numcode
# takes care of it using clearly distinctive characters for both English and Russian
# charset and corrects potential errors
#
# Numcode uses set 21 characters common to Russian and English to encode positive decimal
@sergeych
sergeych / extract_params.rb
Created March 30, 2011 05:12
Extract Hash params from the array if the last element is a Hash
class Array
## Extract Hash params from the array with defaults if the last element is
# a Hash (otherwise returns defaults as is). To be used with *args, e.g.
#
# def function *args
# params = args.extract_params! { :defval => 'foobar' }
# ...
# end
def extract_params! defaults = {}
@sergeych
sergeych / jqtimer.rb
Created April 8, 2011 08:48
Simple handy timer for jquery core.
function Timer(millis, callback, _params) {
var params = $.extend({repeat: false,context: this},_params)
this.interval = millis;
this.repeat = params.repeat;
this.context = params.context;
this.args = params.args;
this.onTimer = callback;
this.callback = $.proxy(this._onTimer, this);
this.single = false;
this._reqs = 0;
@sergeych
sergeych / builder.py
Created June 23, 2011 14:07
fixed builder.py for titanium appcelerator iphone sdk 1.6.2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Build and Launch iPhone Application in Simulator or install
# the application on the device via iTunes
#
import os, sys, uuid, subprocess, shutil, signal, string, traceback, imp
import platform, time, re, run, glob, codecs, hashlib, datetime, plistlib
from compiler import Compiler
@sergeych
sergeych / rpull
Created September 11, 2011 22:14
bash script: git pull && pass rails migrations if need
#!/bin/bash
#
# git pull && run migratoins if need
dbtime=`stat -f "%m" db/migrate`
schema=`stat -f "%m" db/schema.rb`
git pull || exit
# If either schema or migrate dir has been changed, run migrations and test db prepare
@sergeych
sergeych / NameCode32.java
Last active December 27, 2015 08:09
Effectively encodes/decodes binary data to the text suitable to use as urls and file names, unlike Base64 which is case-sensitive
package net.sergeych.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Binary to text encoder suitable to use encoded strings as any parts of urls
* and parts of file names safely even on case-insensitive systems. Human
* friendly, easy to retype from print or from voice, treats confusing
@sergeych
sergeych / httpick.rb
Created December 5, 2015 14:18
Easy took to perform http request with pool and keep-alive support
require 'net/http'
require 'uri'
require 'json'
require 'hashie'
# Simple HTTP access, with embed JSON parsing, pooling and keep-alive support. Especially useful
# for API access over https. Requires json support and gem 'hashie'.
#
# v2 by [email protected]
#
require 'twilio-ruby'
require 'thread'
# Simple interface to send long SMS over twilio and test it like
# ActionMailer. Requires gem twilio-ruby, and following constants from
# your twilio console: SMS_ACCOUNT_SID, SMS_ACCONT_TOKEN,SMS_FROM_NUMBER.
#
# Thread safe.
#
# Enjoy ;) [email protected]