Skip to content

Instantly share code, notes, and snippets.

@kiennt
kiennt / app.rb
Created April 18, 2012 11:07 — forked from cpatni/app.rb
unique calculation using redis
require 'sinatra'
require 'redis'
require 'json'
require 'date'
class String
def &(str)
result = ''
result.force_encoding("BINARY")
@kiennt
kiennt / convert_pdf_to_png.rb
Created January 19, 2013 15:44
Generate jpg image from pdf files
require 'grim'
require 'thread'
class ThreadPool
def initialize(size = 5)
@jobs = Queue.new
@size = size
@pool = Array.new(@size) do |i|
Thread.new do
Thread.current[:id] = i
@kiennt
kiennt / async_upload_s3.rb
Last active December 11, 2015 09:08
Upload to S3 asynchronously with eventmachine and happening library
require 'securerandom'
require 'eventmachine'
require 'happening'
EM.run do
count = 10
pending = count
count.times do |index|
on_error = Proc.new do |http|
@kiennt
kiennt / scope.py
Created January 27, 2013 18:02
Test scope
a = 10
def test():
print a # 10
@kiennt
kiennt / method-missing1.rb
Created February 16, 2013 15:07
Pre define method
require 'benchmark'
class A
def method_missing name, *argv
super unless name =~ /^test$/
"hello world"
end
end
class B
@kiennt
kiennt / pre-define-method-array.rb
Last active December 13, 2015 20:08
Pre define method for array sort
require 'benchmark'
Benchmark.bm do |x|
names = ["matz", "rossum", "ryal", "ritchie", "brendan"]
n = 50000
x.report "method missing" do
class Array
def method_missing name, *argv
super unless name =~ /^sort_by_(\w+)_(asc|desc)$/
@kiennt
kiennt / fibo_array.rb
Last active December 14, 2015 04:49
Fibonaci using array
class FiboUsingArray
include Singleton
def initialize
@fib = [1, 1]
end
def [](n)
if n < @fib.size then
@fib[n]
class FiboUsingDictionary
include Singleton
def initialize
@fib = { 0 => 1, 1 => 1 }
end
def [](n)
return @fib[n] if @fib.include? n
@fib[n] = self[n -1] + self[n - 2]
@kiennt
kiennt / TweetData.scala
Created April 29, 2013 06:43
Coursera scala week 3
package objsets
// real tweet data, collected on Oct 1
object TweetData {
val gizmodo = """[
{ "user": "gizmodo", "text": "Kindle Paperwhite Review: Forget Everything Else, This Is the E-Reader You Want http://t.co/737W6aNC", "retweets": 51.0 },
{ "user": "gizmodo", "text": "These new Apple patents give a sneak peek at what future iPhone cameras might have in store. http://t.co/0YT9rjxp", "retweets": 49.0 },
{ "user": "gizmodo", "text": "Ever wonder why the sky is dark at night? Here's your answer. http://t.co/eTKxkcaE", "retweets": 86.0 },
{ "user": "gizmodo", "text": "The head of Homeland Security stays secure by just not using email, at all. http://t.co/W6KAFEUu", "retweets": 37.0 },
{ "user": "gizmodo", "text": "This is how graphene will grow the flexible semiconductors of the future: http://t.co/IoEvuxp4", "retweets": 43.0 },
@kiennt
kiennt / Huffman.scala
Created April 29, 2013 06:45
Scala coursera Week 4
package patmat
import common._
/**
* Assignment 4: Huffman coding
*
*/
object Huffman {