Skip to content

Instantly share code, notes, and snippets.

@ebisawa
ebisawa / ruby_skype_api_example.rb
Created September 20, 2009 08:34
Skype API example in Ruby
require 'dbus'
MYNAME = 'com.example.skype_api_client'
class SkypeAPI
def initialize(name = MYNAME)
bus = DBus.session_bus
service = bus.service("com.Skype.API")
objs = service.object("/com/Skype")
objs.introspect
@ebisawa
ebisawa / cosine similarity
Created August 31, 2010 07:01
cosine similarity
def vectorize(msg)
vector = {}
words = msg.split(/\s+/)
words.each do |w|
h = w.hash
vector[h] = 1 if vector[h] == nil
vector[h] += 1
end
vector
end
@ebisawa
ebisawa / gist:573139
Created September 10, 2010 05:11
PHP data parser
class PhpDataParser
def initialize(data_string)
@tokens = []
lines = data_string.split("\n")
lines.each do |line|
line.gsub!(%r{//.*$}, '')
@tokens.concat(line.split(/\s+|(=>)/))
end
end
@ebisawa
ebisawa / gantt_start_compare
Created September 28, 2010 05:27
customized gantt_start_compare for Redmine
def gantt_start_compare(x, y)
def cmp(a, b)
if a != nil && b != nil
a <=> b
else
0
end
end
def effective_date(x)
@ebisawa
ebisawa / gist:786115
Created January 19, 2011 12:38
checkall
<input type="checkbox" id="checkall" onchange="
for (i = 0; i < document.f.elements.length; i++) {
if (document.f.elements[i].name == 'selected[]') {
document.f.elements[i].checked = document.getElementById('checkall').checked;
}
}
">
@ebisawa
ebisawa / gist:789298
Created January 21, 2011 05:54
mysql_ruby
require 'mysql'
DB_DEFAULT_USER = 'root'
DB_DEFAULT_PASS = 'hogehoge'
DB_REFRESH = 60 * 60 # 1hour
MYSQL_INT_TYPES = [
Mysql::Field::TYPE_TINY, Mysql::Field::TYPE_SHORT,
Mysql::Field::TYPE_LONG, Mysql::Field::TYPE_INT24,
Mysql::Field::TYPE_LONGLONG, Mysql::Field::TYPE_DECIMAL,
@ebisawa
ebisawa / gist:792986
Created January 24, 2011 09:02
sinatra helpers
helpers do
include Rack::Utils
alias_method :h, :escape_html
alias_method :u, :escape
def partial(renderer, template, options = {})
options = options.merge({:layout => false})
template = "_#{template.to_s}".to_sym
m = method(renderer)
m.call(template, options)
@ebisawa
ebisawa / gist:794565
Created January 25, 2011 05:46
parallel
THREADS = 8
THREAD_TIMEOUT = 180
def xyield(q, &block)
t = Thread.new(Thread.current) do |u|
sleep THREAD_TIMEOUT
puts "-- timeout --"
u.raise
end
@ebisawa
ebisawa / uuid.rb
Last active September 26, 2015 07:57
UUID
class UUID
def initialize
@timestamp = timestamp
@clockseq = clockseq
@node = node
end
def to_s
t = @timestamp
sprintf("%08x-%04x-%04x-%04x-%012x", t[0], t[1], t[2] | version, @clockseq | variant, @node)
@ebisawa
ebisawa / gist:1196608
Created September 6, 2011 04:44
parallel_each
module Enumerable
DEFAULT_PARALLEL = 8
def parallel_each(num = DEFAULT_PARALLEL)
threads = []
queue = Queue.new
num.times do
threads << Thread.new do
while (a = queue.pop) != nil