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
#! ruby19 | |
require 'date' | |
class EndlessEnum | |
include Enumerable | |
def initialize(x, method = :succ) | |
@x = x | |
@method = method |
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
# This class works as a proxy for an instance | |
# and presents it as Hash. | |
class InstanceHash | |
include Enumerable | |
def initialize(obj, default_val = nil, &default_proc) | |
@obj = obj | |
@default = default_val | |
@default_proc = default_proc || lambda {|h,k| @default} | |
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
class Module | |
def attr_with_default(name, default = nil, &init) | |
n = "@#{name}" | |
define_method name do | |
instance_variable_get(n) || (init ? init.call(self) : default) | |
end | |
attr_writer name | |
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
18:00:04 Temp$ allruby sc.rb | |
CYGWIN_NT-5.1 padrklemme1 1.7.5(0.225/5/3) 2010-04-12 19:07 i686 Cygwin | |
======================================== | |
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] | |
Rehearsal ---------------------------------------------- | |
scan 2.766000 0.000000 2.766000 ( 2.786000) | |
scan ++ 4.656000 0.000000 4.656000 ( 4.668000) | |
scan re 2.688000 0.000000 2.688000 ( 2.696000) | |
scan re ++ 4.531000 0.000000 4.531000 ( 4.547000) | |
while 1.094000 0.000000 1.094000 ( 1.135000) |
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
11:01:47 Temp$ ./nested_each.rb | |
[["foo\n", "bar", "dummy"], ["another\nline", "ends", "now\nor not?"]] | |
Now the test... | |
"foo\n" | |
"bar" | |
"dummy" | |
"another\n" | |
"line" |
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
#!/bin/env ruby19 | |
Num = Struct.new :pre, :val do | |
def succ | |
self.class.new pre, val.succ | |
end | |
def to_s; "#{pre}#{val}" end | |
def inspect; "#{self.class.name}(#{self})" 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
#!/bin/env ruby19 | |
require 'nokogiri' | |
# Nokogiri should have this as well | |
REPL = { | |
'<' => '<', | |
'≤' => '<=', | |
'>' => '>', | |
'≥' => '>=', |
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 'pp' | |
Node = Struct.new :value, :parent, :children do | |
def initialize(value = nil, parent = nil) | |
self.value = value | |
self.children = [] | |
yield self if block_given? | |
end | |
def add(child) |
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
package enc; | |
public class EncodingTest { | |
public static void main(String[] args) { | |
boolean lastDefined = !Character.isDefined(0); | |
int lastStart = -1; | |
for (int i = Character.MIN_CODE_POINT; i <= Character.MAX_CODE_POINT; ++i) { | |
final boolean def = Character.isDefined(i); |
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
# test whether the given element is a month label | |
def is_label(o) | |
String === o | |
end | |
# exchange values at positions | |
def swap(arr, i, j) | |
# not using parrallel assignment since | |
# we want to avoid all the fancy stuff | |
x = arr[i] |