From 07bb9f21f56b0d066c44c62b0e6be35eb0e0fd51 Mon Sep 17 00:00:00 2001 From: matz Date: Fri, 13 Aug 1999 05:45:20 +0000 Subject: 1.4.0 git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@520 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- sample/README | 56 ++++++++++++++++++++++++ sample/biorhythm.rb | 10 ++--- sample/cal.rb | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ sample/fib.py | 10 +++++ sample/from.rb | 1 + sample/goodfriday.rb | 35 +++++++++++++++ sample/list.rb | 16 +++---- sample/mine.rb | 42 +++++++++--------- sample/rbc.rb | 8 ++-- sample/rcs.rb | 10 ++--- sample/rename.rb | 14 +++--- sample/sieve.rb | 21 ++++----- sample/test.rb | 97 +++++++++++++++++++++++++---------------- sample/trojan.rb | 3 +- sample/uumerge.rb | 4 +- 15 files changed, 344 insertions(+), 102 deletions(-) create mode 100644 sample/README create mode 100644 sample/cal.rb create mode 100644 sample/fib.py create mode 100644 sample/goodfriday.rb (limited to 'sample') diff --git a/sample/README b/sample/README new file mode 100644 index 000000000..1bdc85f32 --- /dev/null +++ b/sample/README @@ -0,0 +1,56 @@ +README this file +biorhythm.rb biorhythm calculator +cal.rb cal(1) clone +cbreak.rb no echo done by ioctl +clnt.rb socket client +dbmtest.rb test for dbm +dir.rb directory access +dualstack-fetch.rb IPv6 demo +dualstack-httpd.rb IPv6 demo +dstore.rb object database on dbm +eval.rb simple evaluator +export.rb method access example +exyacc.rb extrace BNF from yacc file +fact.rb factorial calculator +fib.awk Fibonacci number (AWK) +fib.pl Fibonacci number (Perl) +fib.py Fibonacci number (Python) +fib.rb Fibonacci number (Ruby) +fib.scm Fibonacci number (Scheme) +freq.rb count word occurrence +from.rb scan mail spool +fullpath.rb convert ls -lR to fullpath format +getopts.test test fot getopt.rb +goodfriday.rb print various christian calendar event. +inf-ruby.el program to run ruby under emacs +io.rb io test +less.rb front end for less +list.rb stupid object sample +list2.rb stupid object sample +list3.rb stupid object sample +mine.rb simple mine sweeper +mkproto.rb extract protptype from C +mpart.rb split file int multi part +mrshtest.rb test marshal +observ.rb observer design pattern sample +occur.pl count word occurrence (Perl) +occur.rb count word occurrence (Ruby) +occur2.rb count word occurrence - another style +philos.rb famous dining philosophers +pi.rb calculate PI +rbc.rb interactive ruby, to be removed by irb +rcs.awk random character stereogram (AWK) +rcs.rb random character stereogram (Ruby) +rcs.dat data for random character stereogram +rd2html.rb rd (Ruby Document) to HTML translator +regx.rb regular expression tester +ruby-mode.el ruby mode for emacs +rubydb2x.el ruby debugger support for emacs 19.2x or before +rubydb3x.el ruby debugger support for emacs 19.3x or later +sieve.rb sieve of Eratosthenes +svr.rb socket server +test.rb test suite used by `make test' +time.rb /usr/bin/time clone +trojan.rb simple tool to find file that may be trojan horse. +tsvr.rb socket server using thread +uumerge.rb merge files and uudecode them diff --git a/sample/biorhythm.rb b/sample/biorhythm.rb index 50ad1f2ef..d2cbb113e 100644 --- a/sample/biorhythm.rb +++ b/sample/biorhythm.rb @@ -19,11 +19,11 @@ def usage() print "Usage:\n" print "biorhythm.rb [options]\n" print " options...\n" - print " -D YYYYMMDD(birthday) : すべて default 値を使う. \n" - print " --sdate | --date YYYYMMDD : system date もしくは指定した日付を使う.\n" - print " --birthday YYYYMMDD : 誕生日の指定をする. \n" - print " -v | -g : Values or Graph の指定. \n" - print " --days DAYS : 期間の指定をする(Graph の時のみ有効). \n" + print " -D YYYYMMDD(birthday) : use default values.\n" + print " --sdate | --date YYYYMMDD : use system date; use specified date.\n" + print " --birthday YYYYMMDD : specifies your birthday.\n" + print " -v | -g : show values or graph.\n" + print " --days DAYS : graph range (only in effect for graphs).\n" print " --help : help\n" end $USAGE = 'usage' diff --git a/sample/cal.rb b/sample/cal.rb new file mode 100644 index 000000000..cf82c6421 --- /dev/null +++ b/sample/cal.rb @@ -0,0 +1,119 @@ +#! /usr/local/bin/ruby + +# cal.rb: Written by Tadayoshi Funaba 1998, 1999 +# $Id: cal.rb,v 1.5 1999/08/04 14:54:18 tadf Exp $ + +require 'date2' + +$tab = +{ + 'cn' => true, # China + 'de' => 2342032, # Germany (protestant states) + 'dk' => 2342032, # Denmark + 'es' => 2299161, # Spain + 'fi' => 2361390, # Finland + 'fr' => 2299227, # France + 'gb' => 2361222, # United Kingdom + 'gr' => 2423868, # Greece + 'hu' => 2301004, # Hungary + 'it' => 2299161, # Italy + 'jp' => true, # Japan + 'no' => 2342032, # Norway + 'pl' => 2299161, # Poland + 'pt' => 2299161, # Portugal + 'ru' => 2421639, # Russia + 'se' => 2361390, # Sweden + 'us' => 2361222, # United States + 'os' => false, # (old style) + 'ns' => true # (new style) +} + +$cc = 'gb' + +def usage + $stderr.puts 'usage: cal [-c iso3166] [-jy] [[month] year]' + exit 1 +end + +def cal(m, y, sg) + for d in 1..31 + break if jd = Date.exist?(y, m, d, sg) + end + fst = cur = Date.new(jd, sg) + ti = Date::MONTHNAMES[m] + ti << ' ' << y.to_s unless $yr + mo = ti.center((($w + 1) * 7) - 1) << "\n" + mo << ['S', 'M', 'Tu', 'W', 'Th', 'F', 'S']. + collect{|x| x.rjust($w)}.join(' ') << "\n" + mo << ' ' * (($w + 1) * fst.wday) + while cur.mon == fst.mon + mo << (if $jd then cur.yday else cur.mday end).to_s.rjust($w) + mo << (if (cur += 1).wday != 0 then "\s" else "\n" end) + end + mo << "\n" * (6 - ((fst.wday + (cur - fst)) / 7)) + mo +end + +def zip(xs) + yr = '' + until xs.empty? + ln = (if $jd then l, r, *xs = xs; [l, r] + else l, c, r, *xs = xs; [l, c, r] end). + collect{|x| x.split(/\n/no, -1)} + 8.times do + yr << ln.collect{|x| + x.shift.ljust((($w + 1) * 7) - 1)}.join(' ') << "\n" + end + end + yr +end + +while /^-([^-].*)$/no =~ $*[0] + a = $1 + if /^c(.+)?$/no =~ a + if $1 + $cc = $1.downcase + elsif $*.length >= 2 + $cc = $*[1].downcase + $*.shift + else + usage + end + else + a.scan(/./no) do |c| + case c + when 'j'; $jd = true + when 'y'; $yr = true + else usage + end + end + end + $*.shift +end +$*.shift if /^--/no =~ $*[0] +usage if (sg = $tab[$cc]).nil? +case $*.length +when 0 + td = Date.today + m = td.mon + y = td.year +when 1 + y = $*[0].to_i + $yr = true +when 2 + m = $*[0].to_i + y = $*[1].to_i +else + usage +end +usage unless m.nil? or (1..12) === m +usage unless y >= -4712 +$w = if $jd then 3 else 2 end +unless $yr + print cal(m, y, sg) +else + print y.to_s.center(((($w + 1) * 7) - 1) * + (if $jd then 2 else 3 end) + + (if $jd then 2 else 4 end)), "\n\n", + zip((1..12).collect{|m| cal(m, y, sg)}), "\n" +end diff --git a/sample/fib.py b/sample/fib.py new file mode 100644 index 000000000..8318021d2 --- /dev/null +++ b/sample/fib.py @@ -0,0 +1,10 @@ +# calculate Fibonacci(20) +# for benchmark +def fib(n): + if n<2: + return n + else: + return fib(n-2)+fib(n-1) + +print fib(20) + diff --git a/sample/from.rb b/sample/from.rb index 93b6c4bad..bed3433f1 100644 --- a/sample/from.rb +++ b/sample/from.rb @@ -17,6 +17,7 @@ class String len += 1 me = self[0, len].ljust(len) if me =~ /.$/ and $&.size == 2 + me[-2..-1] = ' ' me[-2, 2] = ' ' end me.chop! diff --git a/sample/goodfriday.rb b/sample/goodfriday.rb new file mode 100644 index 000000000..cc541af3f --- /dev/null +++ b/sample/goodfriday.rb @@ -0,0 +1,35 @@ +#! /usr/local/bin/ruby + +# goodfriday.rb: Written by Tadayoshi Funaba 1998 +# $Id: goodfriday.rb,v 1.3 1999/08/04 14:54:18 tadf Exp $ + +require 'date2' +require 'holiday' + +es = Date.easter(Date.today.year) +[[-9*7, 'Septuagesima Sunday'], + [-8*7, 'Sexagesima Sunday'], + [-7*7, 'Quinquagesima Sunday (Shrove Sunday)'], + [-48, 'Shrove Monday'], + [-47, 'Shrove Tuesday'], + [-46, 'Ash Wednesday'], + [-6*7, 'Quadragesima Sunday'], + [-3*7, 'Mothering Sunday'], + [-2*7, 'Passion Sunday'], + [-7, 'Palm Sunday'], + [-3, 'Maunday Thursday'], + [-2, 'Good Friday'], + [-1, 'Easter Eve'], + [0, 'Easter Day'], + [1, 'Easter Monday'], + [7, 'Low Sunday'], + [5*7, 'Rogation Sunday'], + [39, 'Ascension Day (Holy Thursday)'], + [42, 'Sunday after Ascension Day'], + [7*7, 'Pentecost (Whitsunday)'], + [50, 'Whitmonday'], + [8*7, 'Trinity Sunday'], + [60, 'Corpus Christi (Thursday after Trinity)']]. +each do |xs| + puts ((es + xs.shift).to_s + ' ' + xs.shift) +end diff --git a/sample/list.rb b/sample/list.rb index 76035e67d..221f7edb1 100644 --- a/sample/list.rb +++ b/sample/list.rb @@ -1,8 +1,8 @@ # Linked list example class MyElem - # オブジェクト生成時に自動的に呼ばれるメソッド + # object initializer called from Class#new def initialize(item) - # @変数はインスタンス変数(宣言は要らない) + # @variables are instance variable, no declaration needed @data = item @succ = nil end @@ -15,7 +15,7 @@ class MyElem @succ end - # 「obj.data = val」としたときに暗黙に呼ばれるメソッド + # the method invoked by ``obj.data = val'' def succ=(new) @succ = new end @@ -40,12 +40,12 @@ class MyList end end - # オブジェクトを文字列に変換するメソッド - # これを再定義するとprintでの表現が変わる + # the method to convert object into string. + # redefining this will affect print. def to_s str = "= @wi then 0 elsif y < 0 then 0 @@ -83,13 +83,13 @@ class Board end end def count(x,y) - # (x,y)$B$KNY@\$9$kGzCF$N?t$rJV$9(B + # (x,y)に隣接する爆弾の数を返す fetch(x-1,y-1)+fetch(x,y-1)+fetch(x+1,y-1)+ fetch(x-1,y) + fetch(x+1,y)+ fetch(x-1,y+1)+fetch(x,y+1)+fetch(x+1,y+1) end def over(win) - # $B%2!<%`$N=*N;(B + # ゲームの終了 quit unless win pos(@cx,@cy); print CHR[11] @@ -100,8 +100,8 @@ class Board end end def over? - # $B%2!<%`$N=*N;%A%'%C%/(B - # $B=*N;=hM}$b8F$S=P$9(B + # ゲームの終了チェック + # 終了処理も呼び出す remain = (@mc+@total == 0) if @over || remain over(remain) @@ -111,8 +111,8 @@ class Board end end def quit - # $B%2!<%`$NCfCG(B($B$^$?$O=*N;(B) - # $BHWLL$rA4$F8+$;$k(B + # ゲームの中断(または終了) + # 盤面を全て見せる @hi.times do|y| pos(0,y) @wi.times do|x| @@ -122,19 +122,19 @@ class Board end end def down - # $B%+!<%=%k$r2<$K(B + # カーソルを下に if @cy < @hi-1 then @cy=@cy+1; pos(@cx, @cy) end end def up - # $B%+!<%=%k$r>e$K(B + # カーソルを上に if @cy > 0 then @cy=@cy-1; pos(@cx, @cy) end end def left - # $B%+!<%=%k$r:8$K(B + # カーソルを左に if @cx > 0 then @cx=@cx-1; pos(@cx, @cy) end end def right - # $B%+!<%=%k$r1&$K(B + # カーソルを右に if @cx < @wi-1 then @cx=@cx+1; pos(@cx, @cy) end end end @@ -143,7 +143,7 @@ bd=Board.new(10,10,10) system("stty raw -echo") begin loop do - case getc + case STDIN.getc when ?n # new game bd.reset when ?m # mark @@ -163,11 +163,11 @@ begin break end if bd.over? - if getc == ?q then break end + if STDIN.getc == ?q then break end bd.reset end end ensure - system("stty -raw echo") + system("stty -raw echo") end print "\n" diff --git a/sample/rbc.rb b/sample/rbc.rb index 83796255c..9f1ab9443 100644 --- a/sample/rbc.rb +++ b/sample/rbc.rb @@ -273,7 +273,7 @@ module BC_APPLICATION__ rests.unshift op identify_number(rests) else - # obj.if などの対応 + # handle ``obj.if'' and such identify_identifier(rests, TRUE) @lex_state = EXPR_ARG end @@ -344,7 +344,7 @@ module BC_APPLICATION__ @lex_state = EXPR_BEG end end - @OP.def_rule('$') do + @OP.def_rule('$') do #' |op, rests| identify_gvar(rests) end @@ -466,7 +466,7 @@ module BC_APPLICATION__ print token, "\n" if CONFIG[:DEBUG] if state = CLAUSE_STATE_TRANS[token] if @lex_state != EXPR_BEG and token =~ /^(if|unless|while|until)/ - # 修飾子 + # modifiers else if ENINDENT_CLAUSE.include?(token) @indent += 1 @@ -640,7 +640,7 @@ module BC_APPLICATION__ def_exception :ErrNodeAlreadyExists, "node already exists" class Node - # postprocがなければ抽象ノード, nilじゃなければ具象ノード + # abstract node if postproc is nil. def initialize(preproc = nil, postproc = nil) @Tree = {} @preproc = preproc diff --git a/sample/rcs.rb b/sample/rcs.rb index 3f74da9ef..7c091936f 100644 --- a/sample/rcs.rb +++ b/sample/rcs.rb @@ -1,12 +1,12 @@ # random dot steraogram # usage: rcs.rb rcs.dat -sw = 40.0 # p^[ -dw = 78.0 # Random Character Streogram +sw = 40.0 # width of original pattern +dw = 78.0 # width of generating Random Character Streogram hdw = dw / 2.0 -w = 20.0 # -h =1.0 # -d = 0.2 # P +w = 20.0 # distance between eyes +h =1.0 # distance from screen and base plane +d = 0.2 # z value unit ss="abcdefghijklmnopqrstuvwxyz0123456789#!$%^&*()-=\\[];'`,./" rnd = srand() diff --git a/sample/rename.rb b/sample/rename.rb index 9abea7e32..cacb2b750 100644 --- a/sample/rename.rb +++ b/sample/rename.rb @@ -140,8 +140,8 @@ gsub!(/\beof_error\b/,"rb_eof_error") gsub!(/\bf_gets\b/,"rb_f_gets") gsub!(/\bio_binmode\b/,"rb_io_binmode") gsub!(/\bio_check_closed\b/,"rb_io_check_closed") -gsub!(/\bio_check_readable\b/,"rb_io_check_readable") -gsub!(/\bio_check_writable\b/,"rb_io_check_writable") +gsub!(/\bio_readable\b/,"rb_io_check_readable") +gsub!(/\bio_writable\b/,"rb_io_check_writable") gsub!(/\bio_close\b/,"rb_io_close") gsub!(/\bio_fptr_finalize\b/,"rb_io_fptr_finalize") gsub!(/\bio_getc\b/,"rb_io_getc") @@ -248,8 +248,8 @@ gsub!(/\bstr_plus\b/,"rb_str_plus") gsub!(/\bstr_resize\b/,"rb_str_resize") gsub!(/\bstr_split\b/,"rb_str_split") gsub!(/\bstr_substr\b/,"rb_str_substr") -gsub!(/\bstr_taint\b/,"rb_str_taint") -gsub!(/\bstr_tainted\b/,"rb_str_tainted") +gsub!(/\bstr_taint\b/,"rb_obj_taint") +gsub!(/\bstr_tainted\b/,"rb_obj_tainted") gsub!(/\bstr_times\b/,"rb_str_times") gsub!(/\bstr_to_str\b/,"rb_str_to_str") gsub!(/\bstr_upto\b/,"rb_str_upto") @@ -283,8 +283,8 @@ gsub!(/\bobj_instance_variables\b/,"rb_obj_instance_variables") gsub!(/\bobj_remove_instance_variable\b/,"rb_obj_remove_instance_variable") gsub!(/\bshow_copyright\b/,"ruby_show_copyright") gsub!(/\bshow_version\b/,"ruby_show_version") -gsub!(/\bdebug\b/,"rb_debug") -gsub!(/\bverbose\b/,"rb_verbose") +gsub!(/\bdebug\b/,"ruby_debug") +gsub!(/\bverbose\b/,"ruby_verbose") gsub!(/\bFail\(/,"rb_raise(rb_eRuntimeError, ") gsub!(/\bArgError\(/,"rb_raise(rb_eArgError, ") gsub!(/\bTypeError\(/,"rb_raise(rb_eTypeError, ") @@ -294,4 +294,4 @@ gsub!(/\bError\b/,"rb_compile_error") gsub!(/\bErrorAppend\b/,"rb_compile_error_append") gsub!(/\bTRUE\b/,"Qtrue") gsub!(/\bFALSE\b/,"Qfalse") -gsub!(/\berrinfo\b/,"rb_errinfo") +gsub!(/\berrinfo\b/,"ruby_errinfo") diff --git a/sample/sieve.rb b/sample/sieve.rb index 5e9f792d8..359c185f2 100644 --- a/sample/sieve.rb +++ b/sample/sieve.rb @@ -1,17 +1,14 @@ # sieve of Eratosthenes -sieve = [] max = Integer(ARGV.shift || 100) +sieve = [] +for i in 2 .. max + sieve[i] = i +end -print "1" -for i in 2 .. max - begin - for d in sieve - fail if i % d == 0 - end - print ", " - print i - sieve.push(i) - rescue +for i in 2 .. Math.sqrt(max) + next unless sieve[i] + (i*i).step(max, i) do |j| + sieve[j] = nil end end -print "\n" +puts sieve.compact.join ", " diff --git a/sample/test.rb b/sample/test.rb index f28327659..a849a42fc 100644 --- a/sample/test.rb +++ b/sample/test.rb @@ -183,7 +183,7 @@ ok(i>4) check "exception"; begin - fail "this must be handled" + raise "this must be handled" ok(false) rescue ok(true) @@ -191,7 +191,7 @@ end $bad = true begin - fail "this must be handled no.2" + raise "this must be handled no.2" rescue if $bad $bad = false @@ -205,9 +205,9 @@ ok(true) $string = "this must be handled no.3" begin begin - fail "exception in rescue clause" + raise "exception in rescue clause" rescue - fail $string + raise $string end ok(false) rescue @@ -217,9 +217,9 @@ end # exception in ensure clause begin begin - fail "this must be handled no.4" + raise "this must be handled no.4" ensure - fail "exception in ensure clause" + raise "exception in ensure clause" end ok(false) rescue @@ -229,7 +229,7 @@ end $bad = true begin begin - fail "this must be handled no.5" + raise "this must be handled no.5" ensure $bad = false end @@ -240,7 +240,7 @@ ok(!$bad) $bad = true begin begin - fail "this must be handled no.6" + raise "this must be handled no.6" ensure $bad = false end @@ -355,7 +355,7 @@ ok($x[1] == 2) ok(begin for k,v in $y - fail if k*2 != v + raise if k*2 != v end true rescue @@ -417,7 +417,7 @@ ok(i == 5) done = true loop{ break - done = false + done = false # should not reach here } ok(done) @@ -427,7 +427,7 @@ loop { break if done done = true next - $bad = true + $bad = true # should not reach here } ok(!$bad) @@ -437,7 +437,7 @@ loop { break if done done = true redo - $bad = true + $bad = true # should not reach here } ok(!$bad) @@ -599,14 +599,29 @@ ok(("abc" =~ /d*$/) == 3) ok("" =~ /^$/) ok("\n" =~ /^$/) ok("a\n\n" =~ /^$/) -"abcabc" =~ /.*a/ -ok($& == "abca") -"abcabc" =~ /.*c/ -ok($& == "abcabc") -"abcabc" =~ /.*?a/ -ok($& == "a") -"abcabc" =~ /.*?c/ -ok($& == "abc") +ok("abcabc" =~ /.*a/ && $& == "abca") +ok("abcabc" =~ /.*c/ && $& == "abcabc") +ok("abcabc" =~ /.*?a/ && $& == "a") +ok("abcabc" =~ /.*?c/ && $& == "abc") +ok(/(.|\n)*?\n(b|\n)/ =~ "a\nb\n\n" && $& == "a\nb") +$x = <