Skip to content

Instantly share code, notes, and snippets.

@bhelx
bhelx / pq.rb
Created July 23, 2012 02:27
Priority Queue with re assignable priorities
class PQueue
def initialize
@q = {}
end
def push(object, priority)
values = @q[priority] ||= []
values.push object
sync
@bhelx
bhelx / rb.js
Created March 23, 2012 19:16
Ruby-esque object construction in javascript
// kind of like ruby openstruct
var OpenObject = function () {
this.construct = function (defaults, params) {
for (var attr in defaults) this[attr] = defaults[attr];
for (var attr in params) this[attr] = params[attr];
};
}
var Circle = function (opts) {
@bhelx
bhelx / pry_trick.rb
Created January 26, 2012 06:37
Pry trick
require 'pry'
class Object
LABELS = [:label1, :label2]
def breakpt(label, target=self)
LABELS.include?(label) ? Pry.start(target) : nil
end
@bhelx
bhelx / reduce_flags.rb
Created January 21, 2012 21:51
reduce bitwise OR flags
REG_EXTENDED = 1
REG_ICASE = (REG_EXTENDED << 1)
REG_NOSUB = (REG_ICASE << 1)
REG_NEWLINE = (REG_NOSUB << 1)
flags = [REG_EXTENDED, REG_NOSUB]
p flags.reduce :|
# => 5
@bhelx
bhelx / libtre_test.c
Created December 19, 2011 19:57
test of libtre fuzzy regex matcher
Compiled Regex
Created Default params
cost_ins -> 1
cost_del -> 1
cost_subst -> 1
max_cost -> 2147483647
max_ins -> 2147483647
max_del -> 2147483647
max_subst -> 2147483647
max_err -> 2147483647
@bhelx
bhelx / index.html
Created December 15, 2011 04:17
"tail -f" a file node 0.6.5 pipe to socket.io,
<!DOCTYPE html>
<html>
<head>
<title>Websockets tail Server</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<style type="text/css" rel="stylesheet">
body{background-color:#222;}
#info{ font-size: 32px; color:#000;text-shadow:#444 1px 1px 2px; text-align:right;margin:20px 10px;text-transform:lowercase;}
@bhelx
bhelx / deep_write.rb
Created October 8, 2011 06:33
write a file in a directory that may not exist
require 'fileutils'
def deep_write(f, data)
FileUtils.mkdir_p(File.dirname(f)) unless File.directory?(File.dirname(f))
File.open(f, 'w').write(data)
end
@bhelx
bhelx / binary_sort.rb
Last active October 10, 2016 20:10
BINARY SORT FOR EXERCISE.
class BinaryTree
attr_accessor :value, :left, :right
def insert(value)
if @value
if value < @value
@left ||= BinaryTree.new
@left.insert(value)
else
@bhelx
bhelx / remembrances.rb
Created May 24, 2011 06:49
ruby learning
# Learning ruby, trying to remember these fun facts
#--ARRAYS
#--#--Useful functions
[1, 2, 3, 4].any? {|n| n > 2} #=> true
[1, 2, 3, 4].all? {|n| n > 2} #=> false
#--#--collect and map (collect! and map!)
a = [ "a", "b", "c", "d" ]
#!/usr/bin/env python
#
# Converts any integer into a base [BASE] number. I have chosen 62
# as it is meant to represent the integers using all the alphanumeric
# characters, [no special characters] = {0..9}, {A..Z}, {a..z}
#
# I plan on using this to shorten the representation of possibly long ids,
# a la url shortenters
#