This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# state lexer | |
require 'strscan' | |
class StateLexer | |
attr_reader :state | |
class ScanError < StandardError; end | |
def initialize opts = {:states => []} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 = /^(?: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'iconv' | |
require 'rubygems' | |
require 'ipparse' | |
write_now = Time.now | |
counts = {} | |
comma = ',' | |
open "member22.csv" do |f| | |
loop do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mixing imperative and functional code | |
sum = 0 | |
(1..10).map do |i| | |
sum += i | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"\xae\xd3\x8f" | |
['AED38F'].pack('H*') | |
"\u4e2d" | |
['4e2d'].pack('H*').force_encoding('utf-16be').encode!('gbk') |
OlderNewer