Skip to content

Instantly share code, notes, and snippets.

View forresty's full-sized avatar

Feng Ye forresty

View GitHub Profile
# http://pragdave.me/blog/2007/12/30/pipelines-using-fibers-in-ruby-19/
# http://pragdave.me/blog/2008/01/01/pipelines-using-fibers-in-ruby-19part-ii/
class PipelineElement
attr_accessor :source
def initialize
@transformer ||= method(:transform)
@filter ||= method(:filter)
@forresty
forresty / sources.list
Last active August 29, 2015 14:16
Ubuntu 14.04 SJTU sources.list
deb http://ftp.sjtu.edu.cn/ubuntu/ trusty main restricted universe multiverse
deb http://ftp.sjtu.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
deb http://ftp.sjtu.edu.cn/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://ftp.sjtu.edu.cn/ubuntu/ trusty main restricted universe multiverse
deb-src http://ftp.sjtu.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://ftp.sjtu.edu.cn/ubuntu/ trusty-security main restricted universe multiverse
#!/usr/bin/env ruby
def to_utf8(codepoint)
case codepoint
when (0..0x7F)
[codepoint]
when (0x80..0x7FF)
[
0b11000000 + (codepoint >> 6),
0b10000000 + (codepoint & 0b111111)
#!/usr/bin/env ruby
def from_utf8(bytes)
def assert_cont_byte(*bytes)
bytes.each do |b|
raise UTF8Error, "not a valid continuation byte: #{b}" unless (128..191).include?(b)
end
end
i = 0
#!/usr/bin/env ruby
encodings = Encoding.list.each_with_object({}) do |enc, full_list|
full_list[enc.name] = [enc.name]
end
Encoding.aliases.each do |alias_name, base_name|
fail "#{base_name} #{alias_name}" unless encodings[base_name]
encodings[base_name] << alias_name
end
# Regional Indicator Symbol
puts "\u{1f1fa 1f1f8}" # US
puts "\u{1f1ef 1f1f5}" # JP
puts "\u{1f1e8 1f1f3}" # CN
puts "\u{1f1ec 1f1e7}" # GB
puts "\u{1f1e9 1f1ea}" # DE
puts "\u{1f1eb 1f1f7}" # FR
puts "\u{1f1ea 1f1f8}" # ES
puts "\u{1f1f7 1f1fa}" # RU
@forresty
forresty / pj.rb
Created March 1, 2015 14:47
a simple and incomplete JSON parser inspired by okjson.rb
#!/usr/bin/env ruby
module PJ
module Parser
extend self
def decode(string)
tokens = lex(string)
raise ParseError, "too few tokens" if tokens.length < 2
class String
def to_proc
lambda { |obj| obj.send(self) }
end
end
p ['a'].map(&"upcase")
# => ["A"]
require File.expand_path(File.dirname(__FILE__) + '/neo')
# Greed is a dice game where you roll up to five dice to accumulate
# points. The following "score" function will be used to calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
module AttributeAddition
def attribute(att, &block)
if att.is_a?(Hash)
att.each do |a, default|
define_attribute(a, default)
end
else
define_attribute(att, &block)
end
end