Skip to content

Instantly share code, notes, and snippets.

@sergueif
sergueif / README
Created September 15, 2009 00:58
local-changes.rb finds all the git repositories it can with "locate" command and checks if you forgot to commit something.
Use this script to find local git repos where you have something uncommitted.
The script searches your hard-drive for repos using the unix "locate" command. To check which directories locate might skip see /etc/updatedb.conf
PRUNEPATHS variable lists directories which are skipped, so remove folders to include them in search.
For more info, run "man locate" and "man updatedb"
# - Place this snippet in you .bashrc or .bash_profile (if you're on a Mac)
# to make bash history much more useful.
# - This basically shares bash history between multiple shell instances and makes the history available everywehere.
# - If your using CTRL-R bash shortcut, this is triples your bash productivity.
#merge history
export HISTCONTROL=erasedups # no duplicate entries
export HISTFILESIZE=10000000 # big big history
export HISTSIZE=1000000 # big big history
# Save and reload the history after each command finishes
def sum_chars(str)
str.split("").inject(0) do |s, c|
s + c[0]
end
end
a = ["hello", "i", "am", "blah"]
sum = (0...(a.size)).inject(0) do |s, i|
s + sum_chars(a[i])*i
require 'rubygems'
require 'algorithms'
def most_common_5(str) #O(n + klog(n))
tally = {} # to count the number of occurences
word = Struct.new(:word, :frequency) #little helper struct
splitter = /[ \*:,\.!\?\n]+/ #to separate words in paragraphs
str.split(splitter).each do |term| #O(n)
tally[term] ||= 0 #initialize to 1 if first found
Character make1(byte b1)
Character make2(byte b1, byte b2)
//helper routine tests if a byte starts with 1-bit
boolean testFor1(byte b) {
if (b & 0b10000000) {
return true;
} else {
return false;
}
@sergueif
sergueif / ruby_by_value.rb
Created October 16, 2010 15:39
ruby by value
#case 1
a = 3
def foo(z)
z = 5
end
def baz(z)
z += 1
end
foo(a)
#environment.rb
require 'active_record'
require 'sqlite3'
require 'logger'
ActiveRecord::Base.configurations = YAML::load(IO.read('config/database.yml'))
ActiveRecord::Base.establish_connection('development')
ActiveRecord::Base.logger = Logger.new('log/debug.log')
development:
adapter: sqlite3
@sergueif
sergueif / dir_ext
Created June 8, 2011 22:16
Methods I wish Dir had
class Dir
def self.current
Dir.new(Dir.pwd)
end
def children
p = path
entries.reject{|f| f =~ /^\..*/ }.map do |name|
child_path = p + "/" + name
if File.directory? child_path
@sergueif
sergueif / kvs.rb
Created June 8, 2011 22:21
Simplest Key-Value store in Ruby
require 'pstore'
class KVS
def self.open(path)
KVS.new(path)
end
def initialize(path)
@pstore = PStore.new path
end
@sergueif
sergueif / test2
Created July 15, 2011 16:17
test2
17
94.94, -28.18
76.47, -113.90
-120.14, -181.20
-115.41, 93.00
-35.11, 59.41
36.24, -179.82
-111.93, 8.18
-127.18, -80.15
80.93, 117.31