Skip to content

Instantly share code, notes, and snippets.

import java.util.logging.Logger
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import org.mortbay.jetty.Server
import org.mortbay.jetty.handler.AbstractHandler
object Test extends AbstractHandler {
val log = Logger.getLogger("handler")
def main(args : Array[String]) = {
@mzsanford
mzsanford / Unicode-normalize-examples.rb
Created July 31, 2009 22:43
Unicode normalization in Ruby sucks
decomposed ="e\xCC\x81"
puts "1: #{decomposed.chars.inspect} // #{decomposed.chars.length}"
puts "2: #{decomposed.chars.normalize(:c).inspect} // #{decomposed.chars.normalize(:c).length}"
puts " 2a: #{decomposed.chars.normalize(:c)[0]}, #{decomposed.chars.normalize(:c)[1]}"
puts " 2b: #{decomposed.chars.normalize(:c).unpack('U*').pack('U')} // #{decomposed.chars.normalize(:c).unpack('U*').pack('U').length}"
puts " 2c: #{decomposed.chars.normalize(:c).unpack('U*').collect{|cp| [cp].pack('U') unless cp.to_s.blank? }} // #{decomposed.chars.normalize(:c).unpack('U*').collect{|cp| [cp].pack('U') unless cp.to_s.blank? }.length}"
puts "3: #{decomposed.chars.normalize(:c).to_s.inspect} // #{decomposed.chars.normalize(:c).to_s.length}"
puts "4: #{decomposed.chars.normalize(:c).to_s.unpack('U'*decomposed.chars.normalize(:c).to_s.length).collect {|x| x.to_s 16}}"
puts "5: #{ActiveSupport::Multibyte::Chars.new(decomposed).length}"
space = " "
japanese_space = [0x3000].pack('U')
puts "---"
puts "Space and \\s : #{!!space.match('\s')}"
puts "Japanese Space and \\s : #{!!japanese_space.match('\s')}"
puts "---"
puts "Space and [[:space:]] : #{!!space.match('[[:space:]]')}"
puts "Japanese Space and [::space:]] : #{!!japanese_space.match('[[:space:]]')}"
latin = "a"
arabic = [0xfeb6].pack('U') # Sheen, ش
cjk = [0x3333].pack('U') # HUIITO, ㌳
kcodes = [ 'NONE', 'u', 's' ]
chars = [latin, arabic, cjk]
patterns = [ /\w/, /\W/, /[[:punct:]]/ ]
kcodes.each do |kcode|
$KCODE = kcode
latin = "6"
egypt = [0x0666].pack('U') # Arabic-Indic 6 - ٦
china = [0x516d].pack('U') # 六
braille = [0x2820].pack('U')
chars = [latin, egypt, china, braille ]
patterns = [ /\d/, /[[:digit:]]/, /[[:alnum:]]/]
chars.each do |char|
# From a .vimrc
# Usage: <ESC>,indent<Enter>
#
map <Leader>indent :s/^\( *[^ ]\)/ \1/<CR>
# Shell commands:
# ∴ sudo gem install twitter-text
# ∴ ruby script/console
Loading development environment (Rails 2.3.5)
>> require 'twitter-text'
=> []
>> include Twitter::Extractor
=> Object
>> urls = extract_urls("This has a url: http://twitter.com")
require 'flockdb'
flock = Flock.new "localhost:7915", :graphs => { :a_to_b => 1, :b_to_c => 2 }
flock.add(1, :a_to_b, 1)
flock.add(1, :a_to_b, 2)
flock.add(1, :a_to_b, 3)
flock.add(2, :b_to_c, 1)
flock.add(2, :b_to_c, 2)
@mzsanford
mzsanford / em-http.rb
Created September 29, 2012 04:23
Scatter Gather HTTP with EventMachine (from a non-EM Ruby class, like Rails)
require 'rubygems'
require 'benchmark'
require 'httparty'
require 'eventmachine'
require 'em-http'
class Interatred
@mzsanford
mzsanford / Sintra Log (subclass) on 3 curl requests
Last active December 17, 2015 14:09
Sintra memory usage - Part 1
∴ ruby subclass.rb
== Sinatra/1.4.2 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on localhost:4567, CTRL+C to stop
---- Init myApp
-- Setting up array id 70252864198560
-- Starting GC
---- Waiting in memory (1848): #<MyApp:0x007fca08831ad8 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>>
---- Waiting in memory (3576): #<MyApp:0x007fca0a0710a8 @default_layout=:layout, @app=nil, @template_cache=#<Tilt::Cache:0x007fca08830f20 @cache={}>, @env={"SERVER_SOFTWARE"=>"thin