Skip to content

Instantly share code, notes, and snippets.

View luikore's full-sized avatar

luikore luikore

View GitHub Profile
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|
@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"
@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 / 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 / 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
require 'iconv'
require 'rubygems'
require 'ipparse'
write_now = Time.now
counts = {}
comma = ','
open "member22.csv" do |f|
loop do
@luikore
luikore / aes128.rb
Created November 2, 2010 18:28
AES128 in Ruby 1.9.2
# coding: utf-8
# Short code to explain the AES128 (Rijndael) cipher
# Some array operations require Ruby 1.9.2+
# Steps defined by http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
class AES128
# Mix Rows coefficients
MIX = [
[2, 3, 1, 1],
@luikore
luikore / fun.rb
Last active September 24, 2015 10:57
functional synopsis
# mixing imperative and functional code
sum = 0
(1..10).map do |i|
sum += i
end
@luikore
luikore / markup.rb
Created December 14, 2010 08:16
fix t.text == nil for Chinese source file. arround gems/gems/rdoc-2.5.11/lib/rdoc/generator/markup.rb:96
require 'rdoc/text'
require 'rdoc/code_objects'
require 'rdoc/generator'
require 'rdoc/markup/to_html_crossref'
##
# Handle common RDoc::Markup tasks for various CodeObjects
module RDoc::Generator::Markup
@luikore
luikore / hex_string.rb
Created December 26, 2010 04:33
hex string to bytes and codepoint
"\xae\xd3\x8f"
['AED38F'].pack('H*')
"\u4e2d"
['4e2d'].pack('H*').force_encoding('utf-16be').encode!('gbk')