Skip to content

Instantly share code, notes, and snippets.

View luikore's full-sized avatar

luikore luikore

View GitHub Profile
require 'iconv'
require 'rubygems'
require 'ipparse'
write_now = Time.now
counts = {}
comma = ','
open "member22.csv" do |f|
loop do
@luikore
luikore / RichNodeSeq.scala
Created September 17, 2009 20:02
scala RichNodeSeq
// extend scala.xml.NodeSeq to support xpath with attr
// call-seq: ex.RichNodeSeq(<a id="2"/><a/>) \ "a[id=2]"
package ex
import scala.xml._
case class RichNodeSeq(nodeSeq: NodeSeq) extends NodeSeq {
def theSeq = nodeSeq.theSeq
private val attrRe = """^\s*(.*)\s*\[\s*(.*)\s*=\s*(.*)\s*\]$""".r
@luikore
luikore / gist:149493
Created July 18, 2009 09:55
chinese string
# regexps to check if a string is pure chinese
class String
# 20k chars
CHINESE_UCS2 = /^(?:
[\x4e-\x9e][\x00-\xff]
|\x9f[\x00-\xa5]
)+$/xn
# 20k chars
CHINESE_UTF8 = /^(?:
@luikore
luikore / state_lexer.rb
Created June 30, 2009 16:21
state lexer
# state lexer
require 'strscan'
class StateLexer
attr_reader :state
class ScanError < StandardError; end
def initialize opts = {:states => []}
@luikore
luikore / 99 bottles of beer
Created June 24, 2009 15:23
99 bottles of beer
# 99 bottles of beer
class Fixnum
def bottle
"#{self} bottles of beer".
sub(/^-1/,'99').
sub(/^0/,'no more').
sub(/^(1\ .+)s/, '\1')
end
def wall
"#{self.bottle} on the wall"
require 'state_machine'
class StateMachine::Machine
def par_event events, test_field, trans
i = 1
# binary 11....1
full = (1 << events.size) - 1
events.each do |e|
j = i
event e do
transition(hash.merge(:if => (lambda{ |obj|