Ruby

登録してみた. http://rubyforge.org/projects/rupy/ RuPy Marshal # marshal_dump.py ... データ作成の為のPython script import sys, marshal def main(filename="test.dump"): data = [ 10, 10.0, -10000, os.environ.get('USER',None), ['foo@example.or…

let binding

def let(*values); yield *values; end def let(*values, &block) block.call(*values) if block_given? end example: let (metainfo['info']) { |info| info['name'] = name info['pieces] = pieces.join info['piece length'] = 2**18 } If write this wit…

ロギングや同期処理をアスペクトにまとめる。 require 'thread' require 'aspectr' include AspectR class MutexArary Aspect def lock(method, object, status, *args) object.mutex.lock end def unlock(method, object, status, *args) object.mutex.unlo…

BitTorrentのメタ情報ファイルや、 トラッカーの応答メッセージで使われている BEncode(bee-encodeと発音する)をRubyで実装してみた。Rythonは、オリジナルのスクリプトに含まれているし、 Perl, OCamlによる実装はすでにあるらしい。&& PHPには、若干仕様が…

The Philosophy of Ruby

Interview on artima.com http://www.artima.com/intv/ruby.html

Rubyだと、既存のクラスにメソッドやイテレータを追加出来る。 これ、Python で出来ないかなぁ。 class Array def inject(n) each { |v| n = yield(n, v) } n end def sum() inject(0) { |n, v| n + v } end end [1,2,3,4,5, 6, 7, 8, 9, 10].sum 55

class Screen def outputToStdout(s) puts s end end class Printer def output(s) end end class PrinterScreenAdapter < Printer def initialize(screen) @screen = screen end def output(s) @screen.outputToStdout(s) end end screen = Screen.new adap…

Iteraror and Generator

#! ruby def fibUpTo(max) a,b = 1,1 while a #! python from __future__ import generators def fibGenerator(num): a,b = 1,1 while a class Fib: def __init__(self, max_num=0) self.max_num = max_num self.i = 1 self.num = 0 def __iter__ (self): re…

abs の実装。Scheme (define (fib n) (cond ((> n 0) n) ((= n 0) 0) ((< n 0) (- n)))) (define (fib n) (if (< n 0) (- n) n)) Ocaml let abs = fun x -> if x < 0 then -x else x;; Python abs1 = lambda x: x < 0 and -x or x def abs2(n) : if n < 0: r…

#!/usr/bin/ruby puts "Hello world";