diff options
Diffstat (limited to 'sample')
46 files changed, 1756 insertions, 941 deletions
diff --git a/sample/MANIFEST b/sample/MANIFEST deleted file mode 100644 index 93c971b114..0000000000 --- a/sample/MANIFEST +++ /dev/null @@ -1,63 +0,0 @@ -MANIFEST -aset.rb -attr.rb -biorhythm.rb -blk.rb -case.rb -cat.rb -cat2.rb -cbreak.rb -clnt.rb -clone.rb -const.rb -dbm.rb -dir.rb -evaldef.rb -export.rb -exyacc.rb -fib.awk -fib.pl -fib.rb -fib.scm -freq.rb -from.rb -fullpath.pl -fullpath.rb -gctest.rb -gctest2.rb -getopts.rb -getopts.test -hash.rb -io.rb -less.rb -list.rb -list2.rb -list3.rb -math.rb -mpart.rb -occur.pl -occur.rb -occur2.rb -opt_s.rb -opt_x.test -parsearg.rb -rcs.awk -rcs.dat -rcs.rb -ruby-mode.el -samp.rb -sieve.rb -split.rb -struct.rb -svr.rb -system.rb -t1.rb -t2.rb -test.rb -time.rb -trap.pl -trap.rb -trojan.pl -trojan.rb -tt.rb -uumerge.rb diff --git a/sample/aset.rb b/sample/aset.rb deleted file mode 100644 index 414c13ba70..0000000000 --- a/sample/aset.rb +++ /dev/null @@ -1,7 +0,0 @@ -# array set example -# output: -# 07045 - -ary = [0, 0, 4, 5] -ary[1, 0] = [7] -print ary, "\n" diff --git a/sample/attr.rb b/sample/attr.rb deleted file mode 100644 index 1d329ea06a..0000000000 --- a/sample/attr.rb +++ /dev/null @@ -1,14 +0,0 @@ -# attribute access example -# output: -# 10 -# #<Foo: @test=10> - -class Foo - attr "test", TRUE -end - -foo = Foo.new -foo.test = 10 -print foo.test, "\n" -foo._inspect.print -print "\n" diff --git a/sample/blk.rb b/sample/blk.rb deleted file mode 100644 index e11cc026ea..0000000000 --- a/sample/blk.rb +++ /dev/null @@ -1,9 +0,0 @@ -def foo() - $block = Block.new -end - -foo(){|i| print "i = ", i, "\n"} -$block.call(2) - -foo(){|i| print "i*2 = ", i*2, "\n"} -$block.call(2) diff --git a/sample/case.rb b/sample/case.rb deleted file mode 100644 index e844cddfd6..0000000000 --- a/sample/case.rb +++ /dev/null @@ -1,14 +0,0 @@ -# case statement example -# output: -# 3..5 - -case "t" -when /1/ - print 1, "\n" -when /t/ - print 3..5, "\n" -when /./ - print 2, "\n" -else - print "else\n" -end diff --git a/sample/cat.rb b/sample/cat.rb deleted file mode 100644 index a3243d308d..0000000000 --- a/sample/cat.rb +++ /dev/null @@ -1,5 +0,0 @@ -# cat -n & `...' operator test -while gets() - if $. == 1 ... ~ /^\*/; print("--") end - printf("%5d: %s", $., $_) -end diff --git a/sample/cat2.rb b/sample/cat2.rb deleted file mode 100644 index bbc1ebb0ff..0000000000 --- a/sample/cat2.rb +++ /dev/null @@ -1,5 +0,0 @@ -# cat -n & `...' operator test -while gets() - if 1 ... /^\*/; print("--") end - printf("%5d: %s", $., $_) -end diff --git a/sample/clnt.rb b/sample/clnt.rb index d2c71ec563..c8c4b2db9f 100644 --- a/sample/clnt.rb +++ b/sample/clnt.rb @@ -1,5 +1,8 @@ # socket example - client side # usage: ruby clnt.rb [host] port + +require "socket" + host=(if $ARGV.length == 2; $ARGV.shift; else "localhost"; end) print("Trying ", host, " ...") STDOUT.flush diff --git a/sample/clone.rb b/sample/clone.rb deleted file mode 100644 index e7d6b00a31..0000000000 --- a/sample/clone.rb +++ /dev/null @@ -1,18 +0,0 @@ -# object cloning & single method test -# output: -# test2 -# test -# test -# clone.rb:18: undefined method `test2' for "#<Object: 0xbfca4>"(Object) -foo = Object.new -def foo.test - print("test\n") -end -bar = foo.clone -def bar.test2 - print("test2\n") -end -bar.test2 -bar.test -foo.test -foo.test2 diff --git a/sample/const.rb b/sample/const.rb deleted file mode 100644 index 50780407b8..0000000000 --- a/sample/const.rb +++ /dev/null @@ -1,24 +0,0 @@ -# constant access test -# output: -# 1234 -# 1268 -TEST1 = 1 -TEST2 = 2 - -module Const - TEST3 = 3 - TEST4 = 4 -end - -module Const2 - TEST3 = 6 - TEST4 = 8 -end - -include Const - -print(TEST1,TEST2,TEST3,TEST4,"\n") - -include Const2 - -print(TEST1,TEST2,TEST3,TEST4,"\n") diff --git a/sample/dbm.rb b/sample/dbm.rb index a2e0659bf5..c77cc2065b 100644 --- a/sample/dbm.rb +++ b/sample/dbm.rb @@ -1,4 +1,6 @@ # ruby dbm acess +require "dbm" + d = DBM.open("test") keys = d.keys if keys.length > 0 then diff --git a/sample/exyacc.rb b/sample/exyacc.rb index cd1170feb4..dafcb037cc 100644 --- a/sample/exyacc.rb +++ b/sample/exyacc.rb @@ -8,15 +8,15 @@ while gets() sbeg = $_.index("\n%%") + 1 send = $_.rindex("\n%%") + 1 $_ = $_[sbeg, send-sbeg] - sub(/.*\n/, "") - gsub(/'{'/, "'\001'") - gsub(/'}'/, "'\002'") - gsub('\*/', "\003\003") - gsub("/\\*[^\003]*\003\003", '') - while gsub(/{[^}{]*}/, ''); end - gsub(/'\001'/, "'{'") - gsub(/'\002'/, "'}'") - while gsub(/^[ \t]*\n(\s)/, '\1'); end - gsub(/([:|])[ \t\n]+(\w)/, '\1 \2') + sub!(/.*\n/, "") + gsub!(/'{'/, "'\001'") + gsub!(/'}'/, "'\002'") + gsub!('\*/', "\003\003") + gsub!("/\\*[^\003]*\003\003", '') + while gsub!(/{[^}{]*}/, ''); end + gsub!(/'\001'/, "'{'") + gsub!(/'\002'/, "'}'") + while gsub!(/^[ \t]*\n(\s)/, '\1'); end + gsub!(/([:|])[ \t\n]+(\w)/, '\1 \2') print $_ end diff --git a/sample/from.rb b/sample/from.rb index f21b1d10f5..2f5fcebe12 100755 --- a/sample/from.rb +++ b/sample/from.rb @@ -1,97 +1,10 @@ #! /usr/local/bin/ruby -module ParseDate - MONTHS = { - 'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, - 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, - 'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12 } - MONTHPAT = MONTHS.keys.join('|') - DAYPAT = 'mon|tue|wed|thu|fri|sat|sun' - - def parsedate(date) - if date.sub(/(#{DAYPAT})/i, ' ') - dayofweek = $1 - end - if date.sub(/\s+(\d+:\d+(:\d+)?)/, ' ') - time = $1 - end - if date =~ /19(\d\d)/ - year = $1 - end - if date.sub(/\s*(\d+)\s+(#{MONTHPAT})\S*\s+/i, ' ') - dayofmonth = $1 - monthname = $2 - elsif date.sub(/\s*(#{MONTHPAT})\S*\s+(\d+)\s+/i, ' ') - monthname = $1 - dayofmonth = $2 - elsif date.sub(/\s*(#{MONTHPAT})\S*\s+(\d+)\D+/i, ' ') - monthname = $1 - dayofmonth = $2 - elsif date.sub(/\s*(\d\d?)\/(\d\d?)/, ' ') - month = $1 - dayofmonth = $2 - end - if monthname - month = MONTHS[monthname.tolower] - end - if ! year && date =~ /\d\d/ - year = $& - end - return year, month, dayofmonth - end - -end +require "parsedate" +require "base64" include ParseDate -def decode64(str) - e = -1; - c = "," - for line in str.split("\n") - line.tr 'A-Za-z0-9+/', "\000-\377" - line.each_byte { |ch| - e+=1 - if e==0 - c = ch << 2 - elsif e==1 - c |= ch >>4 - string += [c].pack('c') - c = ch << 4 - elsif e == 2 - c |= ch >> 2 - string += [c].pack('c'); - c = ch << 6 - elsif e==3 - c |= ch - string += [c].pack('c') - e = -1; - end - } - end - return string; -end - -def j2e(str) - while str =~ /\033\$B([^\033]*)\033\(B/ - s = $1 - pre, post = $`, $' - s.gsub(/./) { |ch| - (ch[0]|0x80).chr - } - str = pre + s + post - end - str -end - -def decode_b(str) - while str =~ /=\?ISO-2022-JP\?B\?(.*)=\?=/ - pre, post = $`, $' - s = decode64($1) - str = pre + s + post - end - j2e(str) -end - if $ARGV[0] == '-w' wait = TRUE $ARGV.shift @@ -100,7 +13,7 @@ end class Mail def Mail.new(f) - if !f.is_kind_of(IO) + if !f.is_kind_of?(IO) f = open(f, "r") me = super f.close @@ -114,9 +27,9 @@ class Mail @header = {} @body = [] while f.gets() - $_.chop + $_.chop! continue if /^From / # skip From-line - break if /^[ \t]*$/ # end of header + break if /^$/ # end of header if /^(\S+):\s*(.*)/ @header[attr = $1.capitalize] = $2 elsif attr @@ -149,17 +62,18 @@ $outcount = 0; def fromout(date, from, subj) return if !date y = m = d = 0 + esc = "\033\(B" y, m, d = parsedate(date) if date from = "sombody@somewhere" if ! from subj = "(nil)" if ! subj from = decode_b(from) subj = decode_b(subj) - printf "%-02d/%02d/%02d [%-28.28s] %-40.40s\n", y, m, d, from, subj + printf "%-02d/%02d/%02d [%-28.28s%s] %-40.40s%s\n",y,m,d,from,esc,subj,esc $outcount += 1 end for file in $ARGV - continue if !File.exists(file) + continue if !File.exists?(file) f = open(file, "r") while !f.eof mail = Mail.new(f) diff --git a/sample/gctest.rb b/sample/gctest.rb deleted file mode 100644 index 6810b95481..0000000000 --- a/sample/gctest.rb +++ /dev/null @@ -1,67 +0,0 @@ -# GC stress test -def cons(car, cdr) - [car, cdr] -end - -def car(x) - if x == nil ; nil else x[0] end -end - -def cdr(x) - if x == nil ; nil else x[1] end -end - -def reverse1(x, y) - if x == nil ; y else reverse1(cdr(x), cons(car(x), y)) end -end - -def reverse(x) - reverse1(x, nil) -end - -def ints(low, up) - if low > up - nil - else - cons(low, ints(low+1, up)) - end -end - -def print_int_list(x) - if x == nil - print("NIL\n") - else - print(car(x)) - if nil != cdr(x) - print(", ") - print_int_list(cdr(x)) - else - print("\n") - end - end -end - -print("start\n") - -a = ints(1, 100) -print_int_list(a) -b = ints(1, 50) -print_int_list(b) -print_int_list(reverse(a)) -print_int_list(reverse(b)) -for i in 1 .. 100 - b = reverse(reverse(b)) -# print(i, ": ") -# print_int_list(b) -end -print("a: ") -print_int_list(a) -print("b: ") -print_int_list(b) -print("reverse(a): ") -print_int_list(reverse(a)) -print("reverse(b): ") -print_int_list(reverse(b)) -a = b = nil -print("finish\n") -GC.start() diff --git a/sample/gctest2.rb b/sample/gctest2.rb deleted file mode 100644 index 851d14f217..0000000000 --- a/sample/gctest2.rb +++ /dev/null @@ -1,71 +0,0 @@ -# GC stress test -def cons(car, cdr) - car::cdr -end - -def car(x) - x.car -end - -def cdr(x) - x.cdr -end - -def reverse1(x, y) - if x == nil then - y - else - reverse1(cdr(x), cons(car(x), y)) - end -end - -def reverse(x) - reverse1(x, nil) -end - -def ints(low, up) - if low > up - nil - else - cons(low, ints(low+1, up)) - end -end - -def print_int_list(x) - if x == nil - print("NIL\n") - else - print(car(x)) - if cdr(x) - print(", ") - print_int_list(cdr(x)) - else - print("\n") - end - end -end - -print("start\n") - -a = ints(1, 100) -print_int_list(a) -b = ints(1, 50) -print_int_list(b) -print_int_list(reverse(a)) -print_int_list(reverse(b)) -for i in 1 .. 100 - b = reverse(reverse(b)) -# print(i, ": ") -# print_int_list(b) -end -print("a: ") -print_int_list(a) -print("b: ") -print_int_list(b) -print("reverse(a): ") -print_int_list(reverse(a)) -print("reverse(b): ") -print_int_list(reverse(b)) -a = b = nil -print("finish\n") -GC.start() diff --git a/sample/getopts.rb b/sample/getopts.rb deleted file mode 100644 index 37fd3dc69d..0000000000 --- a/sample/getopts.rb +++ /dev/null @@ -1,117 +0,0 @@ -# -# getopts.rb - get options -# $Release Version: $ -# $Revision: 1.2 $ -# $Date: 1994/02/15 05:17:15 $ -# by Yasuo OHBA(STAFS Development Room) -# -# -- -# �I�v�V�����̉�͂���, $OPT_?? �ɒl���Z�b�g���܂�. -# �w��̂Ȃ��I�v�V�������w�肳�ꂽ���� nil ��Ԃ��܂�. -# ����I�������ꍇ��, �Z�b�g���ꂽ�I�v�V�����̐���Ԃ��܂�. -# -# getopts(single_opts, *opts) -# -# ex. sample [options] filename -# options ... -# -f -x --version --geometry 100x200 -d unix:0.0 -# �� -# getopts("fx", "version", "geometry:", "d:") -# -# ������: -# -f �� -x (= -fx) �̗l�Ȉꕶ���̃I�v�V�����̎w������܂�. -# �����ň������Ȃ��Ƃ��� nil �̎w�肪�K�v�ł�. -# �������ȍ~: -# �����O�l�[���̃I�v�V������, �����̔����I�v�V�����̎w������܂�. -# --version ��, --geometry 300x400 ��, -d host:0.0 ���ł�. -# �������w��� ":" ��K���t���Ă�������. -# -# �I�v�V�����̎w�肪�������ꍇ, �ϐ� $OPT_?? �� non-nil ��������, ���̃I -# �v�V�����̈������Z�b�g����܂�. -# -f -> $OPT_f = TRUE -# --geometry 300x400 -> $OPT_geometry = 300x400 -# -# - �������� -- ��, ����ȍ~, �S�ăI�v�V�����̉�͂����܂���. -# - -$RCS_ID="$Header: /var/ohba/RCS/getopts.rb,v 1.2 1994/02/15 05:17:15 ohba Exp ohba $" - -def getopts(single_opts, *opts) - if (opts) - single_colon = "" - long_opts = [] - sc = 0 - for option in opts - if (option.length <= 2) - single_colon[sc, 0] = option[0, 1] - sc += 1 - else - long_opts.push(option) - end - end - end - - opts = {} - count = 0 - while ($ARGV.length != 0) - compare = nil - case $ARGV[0] - when /^--?$/ - $ARGV.shift - break - when /^--.*/ - compare = $ARGV[0][2, ($ARGV[0].length - 2)] - if (long_opts != "") - for option in long_opts - if (option[(option.length - 1), 1] == ":" && - option[0, (option.length - 1)] == compare) - if ($ARGV.length <= 1) - return nil - end - eval("$OPT_" + compare + " = " + '$ARGV[1]') - opts[compare] = TRUE - $ARGV.shift - count += 1 - break - elsif (option == compare) - eval("$OPT_" + compare + " = TRUE") - opts[compare] = TRUE - count += 1 - break - end - end - end - when /^-.*/ - for index in 1..($ARGV[0].length - 1) - compare = $ARGV[0][index, 1] - if (single_opts && compare =~ "[" + single_opts + "]") - eval("$OPT_" + compare + " = TRUE") - opts[compare] = TRUE - count += 1 - elsif (single_colon != "" && compare =~ "[" + single_colon + "]") - if ($ARGV[0][index..-1].length > 1) - eval("$OPT_" + compare + " = " + '$ARGV[0][(index + 1)..-1]') - opts[compare] = TRUE - count += 1 - elsif ($ARGV.length <= 1) - return nil - else - eval("$OPT_" + compare + " = " + '$ARGV[1]') - opts[compare] = TRUE - $ARGV.shift - count = count + 1 - end - break - end - end - else - break - end - - $ARGV.shift - if (!opts.includes(compare)) - return nil - end - end - return count -end diff --git a/sample/hash.rb b/sample/hash.rb deleted file mode 100644 index 85f719e7eb..0000000000 --- a/sample/hash.rb +++ /dev/null @@ -1,11 +0,0 @@ -# hash value -# output: -# 78651 -# 78651 -# 78651 -# -45637 - -print(+-1.0.hash,"\n") -print(-1.0.hash,"\n") -print((-1.0).hash,"\n") -print(-(1.0.hash),"\n") diff --git a/sample/list3.rb b/sample/list3.rb index 2c1beb6fa2..1d756fdff0 100644 --- a/sample/list3.rb +++ b/sample/list3.rb @@ -1,5 +1,5 @@ # Linked list example -- short version -# using _inspect +# using inspect class Point def initialize(x, y) @@ -14,5 +14,5 @@ end list1 = [10, 20, Point.new(2, 3), Point.new(4, 5)] list2 = [20, Point.new(4, 5), list1] -print("list1: ", list1._inspect, "\n") -print("list2: ", list2._inspect, "\n") +print("list1: ", list1.inspect, "\n") +print("list2: ", list2.inspect, "\n") diff --git a/sample/marshal.rb b/sample/marshal.rb new file mode 100644 index 0000000000..3d399ffe68 --- /dev/null +++ b/sample/marshal.rb @@ -0,0 +1,13 @@ +require "marshal" +include Marshal +a = 25.6; +pt = Struct.new('point', :x,:y); +x = pt.new(10, 10) +y = pt.new(20, 20) +rt = Struct.new('rectangle', :origin,:corner); +z = rt.new(x, y) +c = Object.new +s = [a, x, z, c, c, "fff"]; +print s.inspect; +d = dumps(s); +print load(d).inspect diff --git a/sample/math.rb b/sample/math.rb deleted file mode 100644 index c0b5225080..0000000000 --- a/sample/math.rb +++ /dev/null @@ -1,4 +0,0 @@ -# math example -include Math -sqrt(4) -print(Math.sqrt(257), "\n") diff --git a/sample/mkproto.rb b/sample/mkproto.rb new file mode 100644 index 0000000000..1d9c9faccb --- /dev/null +++ b/sample/mkproto.rb @@ -0,0 +1,27 @@ +$/ = nil +while gets() + if /^((void|VALUE|int|char *\*|ID|struct [\w_]+ *\*|st_table *\*) *)?\n([\w\d_]+)\(.*\)\n\s*((.+;\n)*){/ + $_ = $' + printf "%s %s(", $2, $3 + args = [] + for arg in $4.split(/;\n\s*/) + arg.gsub! ' +', ' ' + if arg =~ /,/ + if arg =~ /(([^*]+) *\** *[\w\d_]+),/ + type = $2.strip! + args.push $1.strip! + arg = $' + else + type = "" + end + while arg.sub!(/(\** *[\w\d_]+)(,|$)/, "") + args.push type + " " + $1.strip! + end + else + args.push arg.strip! + end + end + printf "%s);\n", args.join(', ') + redo + end +end diff --git a/sample/occur2.rb b/sample/occur2.rb index 8cd5acbe5e..c450c30b0f 100644 --- a/sample/occur2.rb +++ b/sample/occur2.rb @@ -5,7 +5,7 @@ while gets() for word in $_.split(/\W+/) begin freq[word] = freq[word] + 1 - resque + rescue freq[word] = 1 end end diff --git a/sample/opt_s.rb b/sample/opt_s.rb deleted file mode 100644 index 56ff0eea15..0000000000 --- a/sample/opt_s.rb +++ /dev/null @@ -1,12 +0,0 @@ -#! ./ruby -s -# test for option `-s' - -if ($xyz) - print("xyz = TRUE\n") -end -if ($zzz) - print("zzz = ", $zzz, "\n") -end -if ($ARGV.length > 0) - print($ARGV.join(", "), "\n") -end diff --git a/sample/opt_x.test b/sample/opt_x.test deleted file mode 100644 index 47a67f6cfa..0000000000 --- a/sample/opt_x.test +++ /dev/null @@ -1,10 +0,0 @@ -test for option `-x' - -this is a forwarding header -this is a header too. - -from here script starts -#! ./ruby -v -print("tt\n") -__END__ -this is a trailer diff --git a/sample/parsearg.rb b/sample/parsearg.rb deleted file mode 100644 index e7e2b7a7f3..0000000000 --- a/sample/parsearg.rb +++ /dev/null @@ -1,69 +0,0 @@ -# -# parseargs.rb - parse arguments -# $Release Version: $ -# $Revision: 1.3 $ -# $Date: 1994/02/15 05:16:21 $ -# by Yasuo OHBA(STAFS Development Room) -# -# -- -# �����̉�͂���, $OPT_?? �ɒl���Z�b�g���܂�. -# ����I�������ꍇ��, �Z�b�g���ꂽ�I�v�V�����̐���Ԃ��܂�. -# -# parseArgs(argc, single_opts, *opts) -# -# ex. sample [options] filename -# options ... -# -f -x --version --geometry 100x200 -d unix:0.0 -# �� -# parseArgs(1, nil, "fx", "version", "geometry:", "d:") -# -# ������: -# �I�v�V�����ȊO�̍Œ�����̐� -# ������: -# �I�v�V�����̕K�v���c�K���K�v�Ȃ� %TRUE �����łȂ���� %FALSE. -# ��O����: -# -f �� -x (= -fx) �̗l�Ȉꕶ���̃I�v�V�����̎w������܂�. -# �����ň������Ȃ��Ƃ��� nil �̎w�肪�K�v�ł�. -# ��l�����ȍ~: -# �����O�l�[���̃I�v�V������, �����̔����I�v�V�����̎w������܂�. -# --version ��, --geometry 300x400 ��, -d host:0.0 ���ł�. -# �������w��� ":" ��K���t���Ă�������. -# -# �I�v�V�����̎w�肪�������ꍇ, �ϐ� $OPT_?? �� non-nil ��������, ���̃I -# �v�V�����̈������Z�b�g����܂�. -# -f -> $OPT_f = %TRUE -# --geometry 300x400 -> $OPT_geometry = 300x400 -# -# usage ���g�������ꍇ��, $USAGE �� usage() ���w�肵�܂�. -# def usage() -# �c -# end -# $USAGE = 'usage' -# usage ��, --help ���w�肳�ꂽ��, �Ԉ�����w����������ɕ\�����܂�. -# -# - �������� -- ��, ����ȍ~, �S�ăI�v�V�����̉�͂����܂���. -# - -$RCS_ID="$Header: /var/ohba/RCS/parseargs.rb,v 1.3 1994/02/15 05:16:21 ohba Exp ohba $" - -load("getopts.rb") - -def printUsageAndExit() - if $USAGE - apply($USAGE) - end - exit() -end - -def parseArgs(argc, nopt, single_opts, *opts) - if ((noOptions = getopts(single_opts, *opts)) == nil) - printUsageAndExit() - end - if (nopt && noOptions == 0) - printUsageAndExit() - end - if ($ARGV.length < argc) - printUsageAndExit() - end - return noOptions -end diff --git a/sample/rcs.rb b/sample/rcs.rb index faa4606788..13476267b2 100644 --- a/sample/rcs.rb +++ b/sample/rcs.rb @@ -16,7 +16,7 @@ while gets() s = ""; while xr < hdw x = xr * (1 + y) - y * w / 2 - i = (x / (1 + h) + sw /2) + i = (x / (1 + h) + sw / 2) if (1 < i && i < $_.length); c = $_[i, 1].to_i else diff --git a/sample/ruby-mode.el b/sample/ruby-mode.el index bcbbdc35f3..b555994fea 100644 --- a/sample/ruby-mode.el +++ b/sample/ruby-mode.el @@ -12,21 +12,24 @@ ) (defconst ruby-block-mid-re - "else\\|elsif\\|when\\|resque\\|ensure" + "then\\|else\\|elsif\\|when\\|rescue\\|ensure" ) (defconst ruby-block-end-re "end") (defconst ruby-delimiter - (concat "[$/<(){}#\"'`]\\|\\[\\|\\]\\|\\b\\(" - ruby-block-beg-re "\\|" ruby-block-end-re "\\)\\b") + (concat "[?$/(){}#\"'`]\\|\\[\\|\\]\\|\\<\\(" + ruby-block-beg-re "\\|" ruby-block-end-re "\\)\\>") ) (defconst ruby-negative (concat "^[ \t]*\\(\\b\\(" ruby-block-mid-re "\\)\\|\\(" - ruby-block-end-re "\\)\\b\\|\\}\\|\\]\\)") + ruby-block-end-re "\\)\\>\\|\\}\\|\\]\\)") ) +(defconst ruby-operator-chars "[,.+*/%-&|^~=<>:]") +(defconst ruby-symbol-chars "[a-zA-Z0-9_]") + (defvar ruby-mode-abbrev-table nil "Abbrev table in use in ruby-mode buffers.") @@ -53,17 +56,16 @@ (setq ruby-mode-syntax-table (make-syntax-table)) (modify-syntax-entry ?\' "\"" ruby-mode-syntax-table) (modify-syntax-entry ?\" "\"" ruby-mode-syntax-table) -;;(modify-syntax-entry ?\n ">" ruby-mode-syntax-table) -;;(modify-syntax-entry ?\f ">" ruby-mode-syntax-table) (modify-syntax-entry ?# "<" ruby-mode-syntax-table) - (modify-syntax-entry ?$ "/" ruby-mode-syntax-table) + (modify-syntax-entry ?\n ">" ruby-mode-syntax-table) (modify-syntax-entry ?\\ "'" ruby-mode-syntax-table) - (modify-syntax-entry ?_ "w" ruby-mode-syntax-table) + (modify-syntax-entry ?$ "/" ruby-mode-syntax-table) + (modify-syntax-entry ?? "/" ruby-mode-syntax-table) + (modify-syntax-entry ?_ "_" ruby-mode-syntax-table) (modify-syntax-entry ?< "." ruby-mode-syntax-table) (modify-syntax-entry ?> "." ruby-mode-syntax-table) (modify-syntax-entry ?& "." ruby-mode-syntax-table) (modify-syntax-entry ?| "." ruby-mode-syntax-table) - (modify-syntax-entry ?$ "." ruby-mode-syntax-table) (modify-syntax-entry ?% "." ruby-mode-syntax-table) (modify-syntax-entry ?= "." ruby-mode-syntax-table) (modify-syntax-entry ?/ "." ruby-mode-syntax-table) @@ -152,6 +154,19 @@ The variable ruby-indent-level controls the amount of indentation. (indent-to x) (if (> p 0) (forward-char p))))) +(defun ruby-expr-beg () + (save-excursion + (skip-chars-backward " \t") + (or (bolp) (forward-char -1)) + (or (looking-at ruby-operator-chars) + (looking-at "[\\[({]") + (bolp) + (and (looking-at ruby-symbol-chars) + (forward-word -1) + (or + (looking-at ruby-block-beg-re) + (looking-at ruby-block-mid-re)))))) + (defun ruby-parse-region (start end) (let ((indent-point end) (indent 0) @@ -163,113 +178,97 @@ The variable ruby-indent-level controls the amount of indentation. (if start (goto-char start) (ruby-beginning-of-defun)) - (while (and (> indent-point (point)) - (re-search-forward ruby-delimiter indent-point t)) - (let ((w (buffer-substring (match-beginning 0) (match-end 0))) - (pnt (match-beginning 0))) - (cond - ((or (string= "\"" w) ;skip string - (string= "'" w) - (string= "`" w)) - (cond - ((string= w (char-to-string (char-after (point)))) - (forward-char 1)) - ((re-search-forward (format "[^\\]%s" w) indent-point t) + (save-restriction + (narrow-to-region (point) end) + (while (and (> indent-point (point)) + (re-search-forward ruby-delimiter indent-point t)) + (let ((pnt (point)) w) + (goto-char (match-beginning 0)) + (cond + + ((or (looking-at "\"") ;skip string + (looking-at "'") + (looking-at "`")) + (setq w (char-after (point))) + (cond + ((and (not (eobp)) + (equal w (char-after (point))) + (re-search-forward (format "[^\\]%c" w) indent-point t)) nil) - (t - (goto-char indent-point) - (setq in-string t)))) - ((or (string= "/" w) - (string= "<" w)) - (if (string= "<" w) (setq w ">")) - (let (c) - (save-excursion - (goto-char pnt) - (skip-chars-backward " \t") - (setq c (char-after (1- (point)))) - (if c - (setq c (char-syntax c)))) + (t + (goto-char indent-point) + (setq in-string t)))) + ((looking-at "/") + (if (and (ruby-expr-beg) + (goto-char pnt) + (looking-at "\\([^/\n]\\|\\\\/\\)*") + (eq ?/ (char-after (match-end 0)))) + (goto-char (1+ (match-end 0))) + (goto-char indent-point) + (setq in-string t))) + ((looking-at "\\?") ;skip ?char (cond - ((or (eq c ?.) - (and (eq c ?w) - (save-excursion - (forward-word -1) - (or - (looking-at ruby-block-beg-re) - (looking-at ruby-block-mid-re))))) - (if (search-forward w indent-point t) - nil - (goto-char indent-point) - (setq in-string t)))))) - ((string= "$" w) ;skip $char - (forward-char 1)) - ((string= "#" w) ;skip comment - (forward-line 1)) - ((string= "(" w) ;skip to matching paren - (let ((orig depth)) - (setq nest (cons (point) nest)) - (setq depth (1+ depth)) - (while (and (/= depth orig) - (re-search-forward "[()]" indent-point t)) - (cond - ((= (char-after (match-beginning 0)) ?\( ) - (setq nest (cons (point) nest)) - (setq depth (1+ depth))) - (t - (setq nest (cdr nest)) - (setq depth (1- depth))))) - (if (> depth orig) (setq in-paren ?\()))) - ((string= "[" w) ;skip to matching paren - (let ((orig depth)) - (setq nest (cons (point) nest)) - (setq depth (1+ depth)) - (while (and (/= depth orig) - (re-search-forward "\\[\\|\\]" indent-point t)) - (cond - ((= (char-after (match-beginning 0)) ?\[ ) - (setq nest (cons (point) nest)) - (setq depth (1+ depth))) - (t - (setq nest (cdr nest)) - (setq depth (1- depth))))) - (if (> depth orig) (setq in-paren ?\[)))) - ((string= "{" w) ;skip to matching paren - (let ((orig depth)) - (setq nest (cons (point) nest)) + ((ruby-expr-beg) + (looking-at "?\\(\\\\C-\\|\\\\M-\\)*.") + (goto-char (match-end 0))) + (t + (goto-char pnt)))) + ((looking-at "\\$") ;skip $char + (goto-char pnt) + (forward-char 1)) + ((looking-at "#") ;skip comment + (forward-line 1) + (goto-char pnt)) + ((looking-at "[\\[({]") + (setq nest (cons (cons (char-after (point)) pnt) nest)) (setq depth (1+ depth)) - (while (and (/= depth orig) - (re-search-forward "[{}]" indent-point t)) - (cond - ((= (char-after (match-beginning 0)) ?{ ) - (setq nest (cons (point) nest)) - (setq depth (1+ depth))) - (t - (setq nest (cdr nest)) - (setq depth (1- depth))))) - (if (> depth orig) (setq in-paren ?{)))) - ((string-match ruby-block-end-re w) - (setq nest (cdr nest)) - (setq depth (1- depth))) - ((string-match ruby-block-beg-re w) - (let (c) - (save-excursion - (goto-char pnt) - (skip-chars-backward " \t") - (setq c (char-after (1- (point))))) - (if (or (null c) (= c ?\n) (= c ?\;)) - (progn - (setq nest (cons (point) nest)) - (setq depth (1+ depth)))))) - (t - (error (format "bad string %s" w))))))) - (list in-string in-paren (car nest) depth))) + (goto-char pnt)) + ((looking-at "[])}]") + (setq nest (cdr nest)) + (setq depth (1- depth)) + (goto-char pnt)) + ((looking-at ruby-block-end-re) + (if (and (not (bolp)) + (progn + (forward-char -1) + (eq ?_ (char-after (point)))) + (progn + (goto-char pnt) + (eq ?_ (char-after (point))))) + nil + (setq nest (cdr nest)) + (setq depth (1- depth))) + (goto-char pnt)) + ((looking-at ruby-block-beg-re) + (and + (or (bolp) + (progn + (forward-char -1) + (not (eq ?_ (char-after (point)))))) + (save-excursion + (goto-char pnt) + (not (eq ?_ (char-after (point))))) + (skip-chars-backward " \t") + (or (bolp) + (save-excursion + (forward-char -1) + (looking-at ruby-operator-chars))) + (progn + (setq nest (cons (cons nil pnt) nest)) + (setq depth (1+ depth)))) + (goto-char pnt)) + (t + (error (format "bad string %s" + (buffer-substring (point) pnt) + ))))))) + (list in-string (car nest) depth)))) (defun ruby-calculate-indent (&optional parse-start) (save-excursion (beginning-of-line) (let ((indent-point (point)) (case-fold-search nil) - state eol + state bol eol (indent 0)) (if parse-start (goto-char parse-start) @@ -281,25 +280,74 @@ The variable ruby-indent-level controls the amount of indentation. (setq indent nil)) ; do nothing ((nth 1 state) ; in paren - (goto-char (nth 2 state)) + (goto-char (cdr (nth 1 state))) (setq indent - (if (and (eq (nth 1 state) ?\( ) (not (looking-at "$"))) + (if (and (eq (car (nth 1 state)) ?\( ) + (not (looking-at "(\\s *$"))) (current-column) (+ (current-indentation) ruby-indent-level)))) - ((> (nth 3 state) 0) ; in nest - (goto-char (nth 2 state)) + ((> (nth 2 state) 0) ; in nest + (goto-char (cdr (nth 1 state))) (forward-word -1) ; skip back a keyword (setq indent (+ (current-column) ruby-indent-level))) (t ; toplevel (setq indent 0))) - (goto-char indent-point) - (end-of-line) - (setq eol (point)) - (beginning-of-line) - (if (re-search-forward ruby-negative eol t) + + (cond + (indent + (goto-char indent-point) + (end-of-line) + (setq eol (point)) + (beginning-of-line) + (cond + ((re-search-forward ruby-negative eol t) (setq indent (- indent ruby-indent-level))) + ;;operator terminated lines + ((and + (save-excursion + (beginning-of-line) + (not (bobp))) + (or (null (car (nth 1 state))) ;not in parens + (and (eq (car (nth 1 state)) ?\{) + (save-excursion ;except non-block braces + (goto-char (cdr (nth 1 state))) + (or (bobp) (forward-char -1)) + (not (ruby-expr-beg)))))) + (beginning-of-line) + (skip-chars-backward " \t\n") + (beginning-of-line) ; goto beginning of non-empty line + (setq bol (point)) + (end-of-line) + (setq eol (point)) + (and (search-backward "#" bol t) ; check for comment line + (not (eq ?? (char-after (1- (point))))) + (not (nth 0 (ruby-parse-region parse-start (point)))) + (setq eol (point))) + (goto-char eol) + (skip-chars-backward " \t") + (or (bobp) (forward-char -1)) + (and (looking-at ruby-operator-chars) +;; (or (not (eq ?/ (char-after (point)))) +;; (progn +;; (not (nth 0 (ruby-parse-region parse-start (point)))))) + (or (not (eq ?/ (char-after (point)))) + (null (nth 0 (ruby-parse-region parse-start (point))))) + (save-excursion + (goto-char parse-start) + (sit-for 1)) + (not (eq (char-after (1- (point))) ?$)) + (or (not (eq ?| (char-after (point)))) + (save-excursion + (or (eolp) (forward-char -1)) + (and (search-backward "|" bol t) + (skip-chars-backward " \t\n") + (and (not (eolp)) + (progn + (forward-char -1) + (not (looking-at "\\{"))))))) + (setq indent (+ indent ruby-indent-level))))))) indent))) (defun ruby-electric-brace (arg) @@ -329,7 +377,7 @@ An end of a defun is found by moving forward from the beginning of one." (interactive "*") (save-excursion (delete-region (point) (progn (skip-chars-backward " \t") (point)))) - (insert ?\n) + (newline) (save-excursion (forward-line -1) (indent-according-to-mode)) @@ -348,3 +396,26 @@ An end of a defun is found by moving forward from the beginning of one." (goto-char beg) (while (re-search-forward "^\\([ \t]*\\)#" end t) (replace-match "\\1" nil nil)))) + +(if (featurep 'hilit19) + (hilit-set-mode-patterns + 'ruby-mode + '(("\\s #.*$" nil comment) + ("^#.*$" nil comment) + ("\\$\\(.\\|\\sw+\\)" nil type) + ("[^$\\?]\\(\"[^\\\"]*\\(\\\\\\(.\\|\n\\)[^\\\"]*\\)*\"\\)" 1 string) + ("[^$\\?]\\('[^\\']*\\(\\\\\\(.\\|\n\\)[^\\']*\\)*'\\)" 1 string) + ("^/\\([^/\n]\\|\\\\/\\)*/" nil string) + ("[^a-zA-Z_]\\s *\\(/\\([^/\n]\\|\\\\/\\)*/\\)" 1 string) + ("\\(begin\\|case\\|else\\|elsif\\|end\\|ensure\\|for\\|if\\|rescue\\|then\\|when\\|while\\)\\s *\\(/\\([^/\n]\\|\\\\/\\)*/\\)" 2 string) + ("^\\s *require.*$" nil include) + ("^\\s *load.*$" nil include) + ("^\\s *\\(include\\|alias\\|undef\\).*$" nil decl) + ("^\\s *\\<\\(class\\|def\\|module\\)\\>" "[)\n;]" defun) + ("[^_]\\<\\(begin\\|case\\|else\\|elsif\\|end\\|ensure\\|for\\|if\\|rescue\\|then\\|when\\|while\\)\\>[^_]" 1 defun) + ("[^_]\\<\\(and\\|break\\|continue\\|fail\\|in\\|not\\|or\\|redo\\|retry\\|return\\|super\\|yield\\)\\>[^_]" 1 keyword) + ("[^_]\\<\\(self\\|nil\\|TRUE\\|FALSE\\|__LINE__\\|__FILE__\\)\\>[^_]" 1 define) + ("$.[a-zA-Z_0-9]*" nil struct) + ("@[a-zA-Z_0-9]+" nil struct) + ("[^_]\\<[A-Z].[a-zA-Z_0-9]*" nil define) + ("^__END__" nil label)))) diff --git a/sample/samp.rb b/sample/samp.rb deleted file mode 100644 index 4052a308c1..0000000000 --- a/sample/samp.rb +++ /dev/null @@ -1,15 +0,0 @@ -# �S�p�������܂ރX�N���v�g -# �g����: samp.rb file.. - -�P�� = 0 -while gets() - printf("%3d: %s", $., $_) - while sub(/\w+/, '') - if $& != ""; - �P�� += 1 - end - end - if ($. >= 10); break; end -end -printf("line: %d\n", $.) -printf("word: %d\n", �P��) diff --git a/sample/sieve.rb b/sample/sieve.rb index 640cc32b08..a953784284 100644 --- a/sample/sieve.rb +++ b/sample/sieve.rb @@ -12,7 +12,7 @@ for i in 2 .. max print ", " print i sieve.push(i) - resque + rescue end end print "\n" diff --git a/sample/split.rb b/sample/split.rb deleted file mode 100644 index 2b6f3921ce..0000000000 --- a/sample/split.rb +++ /dev/null @@ -1,13 +0,0 @@ -# split test -print("1 byte string", "\n") -print("1 byte string".reverse, "\n") - -print("����ʸ����", "\n") -print("����ʸ����".reverse, "\n") - -print("1 byte string", "\n") -print("1 byte string".split(//).reverse.join(":"), "\n") -print("����ʸ����", "\n") -print("����ʸ����".split(//).reverse.join(":"), "\n") -print("���Ѥ�1byte�κ���", "\n") -print("���Ѥ�1byte�κ���".split(//).reverse.join(":"), "\n") diff --git a/sample/struct.rb b/sample/struct.rb deleted file mode 100644 index 322764d02f..0000000000 --- a/sample/struct.rb +++ /dev/null @@ -1,8 +0,0 @@ -#output: -# struct test -# 1 - -foo = Struct.new("test", "a1"::1, "a2"::2) -print(foo, "\n") -bar = foo.clone -print(bar.a1, "\n") diff --git a/sample/svr.rb b/sample/svr.rb index 23b2bf71f6..460c16bedf 100644 --- a/sample/svr.rb +++ b/sample/svr.rb @@ -1,6 +1,8 @@ # socket example - server side # usage: ruby svr.rb +require "socket" + gs = TCPserver.open(0) addr = gs.addr addr.shift diff --git a/sample/system.rb b/sample/system.rb deleted file mode 100644 index 02f3782b39..0000000000 --- a/sample/system.rb +++ /dev/null @@ -1,2 +0,0 @@ -# command string -print(`echo foobar`) diff --git a/sample/t1.rb b/sample/t1.rb deleted file mode 100644 index 701a1cd389..0000000000 --- a/sample/t1.rb +++ /dev/null @@ -1,20 +0,0 @@ -def test(a1, *a2) - while 1 - case gets() - when nil - break - when /^-$/ - print("-\n") - return - when /^-help/ - print("-help\n") - break - end - end - print(a1, a2, "\n") -end - -print($ARGV, "\n") -print("in: ") -test(1) -print("end\n") diff --git a/sample/t2.rb b/sample/t2.rb deleted file mode 100644 index 2e3741e8f1..0000000000 --- a/sample/t2.rb +++ /dev/null @@ -1,23 +0,0 @@ -#print("in Print\n") -def t2() end - -def println(*args) - for a in args - t2() - print(a) - end - print("\n") -end - -def tt - for i in 1..10 - println("i:", i); - yield(i); - end -end - -test = tt{|i| - if i == 3; break end - println("ttt: ", i); -} -#exit() diff --git a/sample/test.rb b/sample/test.rb index 9c422cc94a..7f26433181 100644 --- a/sample/test.rb +++ b/sample/test.rb @@ -1,5 +1,1040 @@ -index = 1 -for argument in $ARGV - printf("����%d:%s\n", index, argument) - index = index + 1 +#! /usr/local/bin/ruby + +$testnum=0 + +def check(what) + printf "%s\n", what + $what = what + $testnum = 0 end + +def ok + $testnum+=1 + printf "ok %d\n", $testnum +end + +def notok + $testnum+=1 + printf "not ok %s %d\n", $what, $testnum + $failed = TRUE +end + +# make sure conditional operators work + +check "condition" + +$x = '0'; + +$x == $x && ok +$x != $x && notok +$x == $x || notok +$x != $x || ok + +# first test to see if we can run the tests. + +check "if"; + +$x = 'test'; +if $x == $x then ok else notok end +if $x != $x then notok else ok end + +check "case" + +case 5 +when 1, 2, 3, 4, 6, 7, 8 + notok +when 5 + ok +end + +case 5 +when 5 + ok +when 1..10 + notok +end + +case 5 +when 5 + ok +else + notok +end + +case "foobar" +when /^f.*r$/ + ok +else + notok +end + +check "while"; + +tmp = open("while_tmp", "w") +tmp.print "tvi925\n"; +tmp.print "tvi920\n"; +tmp.print "vt100\n"; +tmp.print "Amiga\n"; +tmp.print "paper\n"; +tmp.close + +# test break + +tmp = open("while_tmp", "r") + +while tmp.gets() + break if /vt100/ +end + +if !tmp.eof && /vt100/ then + ok +else + notok +end +tmp.close + +# test continue +$bad = FALSE +tmp = open("while_tmp", "r") +while tmp.gets() + continue if /vt100/; + $bad = 1 if /vt100/; +end +if !tmp.eof || /vt100/ || $bad + notok +else + ok +end +tmp.close + +# test redo +$bad = FALSE +tmp = open("while_tmp", "r") +while tmp.gets() + if gsub!('vt100', 'VT100') + gsub!('VT100', 'Vt100') + redo; + end + $bad = 1 if /vt100/; + $bad = 1 if /VT100/; +end +if !tmp.eof || $bad + notok +else + ok +end +tmp.close + +# test interval +$bad = FALSE +tmp = open("while_tmp", "r") +while tmp.gets() + break if not 1..2 + if /vt100/ || /Amiga/ || /paper/ + $bad = TRUE + notok + break + end +end +ok if not $bad +tmp.close + +File.unlink "while_tmp" or `/bin/rm -f "while_tmp"` + +# exception handling +check "exception"; + +begin + fail "this must be handled" + notok +rescue + ok +end + +$bad = TRUE +begin + fail "this must be handled no.2" +rescue + if $bad + $bad = FALSE + retry + notok + end +end +ok + +$bad = TRUE +$string = "this must be handled no.3" +begin + fail $string +rescue +ensure + $bad = FALSE + ok +end +notok if $bad || $! != $string + +# exception in rescue clause +begin + begin + fail "this must be handled no.4" + rescue + fail "exception in rescue clause" + end + notok +rescue + ok +end + +check "array" +$x = [0, 1, 2, 3, 4, 5] +if $x[2] == 2 + ok +else + notok +end + +if $x[1..3] == [1, 2, 3] + ok +else + notok +end + +if $x[1,3] == [1, 2, 3] + ok +else + notok +end + +if [1, 2] + [3, 4] == [1, 2, 3, 4] + ok +else + notok +end + +$x[0, 2] = 10 +if $x[0] == 10 && $x[1] == 2 + ok +else + notok +end + +$x[0, 0] = -1 +if $x[0] == -1 && $x[1] == 10 + ok +else + notok +end + +$x[-1, 1] = 20 +if $x[-1] == 20 && $x.pop == 20 + ok +else + notok +end + +$x = ["it", "came", "to", "pass", "that", "..."] +$x = $x.sort.join(" ") +if $x == "... came it pass that to" + ok +else + notok +end + +# split test +if "1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1" + ok +else + notok +end + +$x = [1] +if ($x * 5).join(":") == '1:1:1:1:1' then ok else notok end +if ($x * 1).join(":") == '1' then ok else notok end +if ($x * 0).join(":") == '' then ok else notok end + +check "hash" +$x = {1=>2, 2=>4, 3=>6} +$y = {1, 2, 2, 4, 3, 6} + +if $x[1] == 2 + ok +else + notok +end + +begin + for k,v in $y + fail if k*2 != v + end + ok +rescue + notok +end + +if $x.length == 3 + ok +else + notok +end + +if $x.has_key?(1) + ok +else + notok +end + +if $x.has_value?(4) + ok +else + notok +end + +if $x.indexes(2,3) == [4,6] + ok +else + notok +end + +$z = $y.keys.join(":") +if $z == "1:2:3" + ok +else + notok +end + +$z = $y.values.join(":") +if $z == "2:4:6" + ok +else + notok +end + +if $x == $y + ok +else + notok +end + +$y.shift +if $y.length == 2 + ok +else + notok +end + +check "iterator" + +if iterator? then notok else ok end + +def ttt + if iterator? then ok else notok end +end +ttt{} + +# yield at top level +begin + yield + notok +rescue + ok +end + +$x = [1, 2, 3, 4] +$y = [] + +# iterator over array +for i in $x + $y.push i +end +if $x == $y + ok +else + notok +end + +# nested iterator +def tt + 1.upto(10) {|i| + yield i + } +end + +tt{|i| break if i == 5} +if i == 5 + ok +else + notok +end + +# iterator break/redo/continue/retry +done = TRUE +loop{ + break + done = FALSE + notok +} +ok if done + +done = TRUE +$bad = FALSE +loop { + break if not done + done = FALSE + continue + $bad = TRUE +} +if $bad + notok +else + ok +end + +done = TRUE +$bad = FALSE +loop { + break if not done + done = FALSE + redo + $bad = TRUE +} +if $bad + notok +else + ok +end + +$x = [] +for i in 1 .. 7 + $x.push(i) +end +if $x.size == 7 + ok +else + notok +end +# $x == [1, 2, 3, 4, 5, 6, 7] +$done = FALSE +$x = [] +for i in 1 .. 7 # see how retry works in iterator loop + if i == 4 and not $done + $done = TRUE + retry + end + $x.push(i) +end +# $x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7] +if $x.size == 10 + ok +else + notok +end + +check "bignum" +def fact(n) + return 1 if n == 0 + return n*fact(n-1) +end +if fact(40) == 815915283247897734345611269596115894272000000000 + ok +else + notok +end +if fact(40) == 815915283247897734345611269596115894272000000001 + notok +else + ok +end + +check "string & char" + +if "abcd" == "abcd" + ok +else + notok +end + +if "abcd" =~ "abcd" + ok +else + notok +end + +$foo = "abc" +if "#$foo = abc" == "abc = abc" + ok +else + notok +end + +if "#{$foo} = abc" == "abc = abc" + ok +else + notok +end + +foo = "abc" +if "#{foo} = abc" == "abc = abc" + ok +else + notok +end + +if '-' * 5 == '-----' then ok else notok end +if '-' * 1 == '-' then ok else notok end +if '-' * 0 == '' then ok else notok end + +foo = '-' +if foo * 5 == '-----' then ok else notok end +if foo * 1 == '-' then ok else notok end +if foo * 0 == '' then ok else notok end + +# character constants(assumes ASCII) +if "a"[0] == ?a + ok +else + notok +end + +if ?a == ?a + ok +else + notok +end + +if ?\C-a == 1 + ok +else + notok +end + +if ?\M-a == 225 + ok +else + notok +end + +if ?\M-\C-a == 129 + ok +else + notok +end + +$x = "abcdef" +$y = [ ?a, ?b, ?c, ?d, ?e, ?f ] +$bad = FALSE +$x.each_byte {|i| + if i != $y.shift + $bad = TRUE + break + end +} +if not $bad + ok +else + notok +end + +check "asignment" +a = nil +if a == nil + ok +else + notok +end + +a, b = 1, 2 +if a == 1 and b == 2 then + ok +else + notok +end + +a, *b = 1, 2, 3 +if a == 1 and b == [2, 3] then + ok +else + notok +end + +check "call" +def aaa(a, b=100, *rest) + res = [a, b] + res += rest if rest + return res +end + +begin + aaa() + notok +rescue + ok +end + +begin + aaa + notok +rescue + ok +end + +begin + if aaa(1) == [1, 100] + ok + else + fail + end +rescue + notok +end + +begin + if aaa(1, 2) == [1, 2] + ok + else + fail + end +rescue + notok +end + +begin + if aaa(1, 2, 3, 4) == [1, 2, 3, 4] + ok + else + fail + end +rescue + notok +end + +begin + if aaa(1, *[2, 3, 4]) == [1, 2, 3, 4] + ok + else + fail + end +rescue + notok +end + +check "proc" +$proc = proc{|i| i} +if $proc.call(2) == 2 + ok +else + notok +end + +$proc = proc{|i| i*2} +if $proc.call(2) == 4 + ok +else + notok +end + +proc{ + iii=5 # dynamic local variable + $proc = proc{ |i| + iii = i + } + $proc2 = proc { + $x = iii # dynamic variables shared by procs + } + if defined?(iii) # dynamic variables' scope + ok + else + notok + end +}.call +if defined?(iii) # out of scope + notok +else + ok +end +$x=0 +$proc.call(5) +$proc2.call +if $x == 5 + ok +else + notok +end + +check "signal" +begin + kill "SIGINT", $$ + sleep 1 + notok +rescue + ok +end + +$x = 0 +trap "SIGINT", proc{|sig| $x = sig;fail} +begin + kill "SIGINT", $$ + sleep 1 + notok +rescue + if $x == 2 + ok + else + notok + end +end + +$x = FALSE +trap "SIGINT", "$x = TRUE;fail" +begin + kill "SIGINT", $$ + sleep 1 + notok +rescue + if $x + ok + else + notok + end +end + +check "eval" +$bad=FALSE +eval 'while FALSE; $bad = TRUE; print "foo\n" end +if not $bad then ok else notok end' + +$foo = 'ok' +begin + eval $foo +rescue + notok +end + +check "system" +if `echo foobar` == "foobar\n" + ok +else + notok +end + +if `./ruby -e 'print "foobar"'` == 'foobar' + ok +else + notok +end + +tmp = open("script_tmp", "w") +tmp.print "print $zzz\n"; +tmp.close + +if `./ruby -s script_tmp -zzz` == 't' + ok +else + notok +end + +if `./ruby -s script_tmp -zzz=555` == '555' + ok +else + notok +end + +tmp = open("script_tmp", "w") +tmp.print "#! /usr/local/bin/ruby -s\n"; +tmp.print "print $zzz\n"; +tmp.close + +if `./ruby script_tmp -zzz=678` == '678' + ok +else + notok +end + +tmp = open("script_tmp", "w") +tmp.print "this is a leading junk\n"; +tmp.print "#! /usr/local/bin/ruby -s\n"; +tmp.print "print $zzz\n"; +tmp.print "__END__\n"; +tmp.print "this is a trailing junk\n"; +tmp.close + +if `./ruby -x script_tmp` == 'nil' + ok +else + notok +end + +if `./ruby -x script_tmp -zzz=555` == '555' + ok +else + notok +end + +tmp = open("script_tmp", "w") +for i in 1..5 + tmp.print i, "\n" +end +tmp.close + +`./ruby -i.bak -pe 'sub(/^[0-9]+$/){$&.to_i * 5}' script_tmp` +done = TRUE +tmp = open("script_tmp", "r") +while tmp.gets + if $_.to_i % 5 != 0 + done = FALSE + notok + break + end +end +ok if done + +File.unlink "script_tmp" or `/bin/rm -f "script_tmp"` +File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"` + +check "const" +TEST1 = 1 +TEST2 = 2 + +module Const + TEST3 = 3 + TEST4 = 4 +end + +module Const2 + TEST3 = 6 + TEST4 = 8 +end + +include Const + +if [TEST1,TEST2,TEST3,TEST4] == [1,2,3,4] + ok +else + notok +end + +include Const2 + +if [TEST1,TEST2,TEST3,TEST4] == [1,2,6,8] + ok +else + notok +end + +check "clone" +foo = Object.new +def foo.test + "test" +end +bar = foo.clone +def bar.test2 + "test2" +end + +if bar.test2 == "test2" + ok +else + notok +end + +if bar.test == "test" + ok +else + notok +end + +if foo.test == "test" + ok +else + notok +end + +begin + foo.test2 + notok +rescue + ok +end + +check "pack" + +$format = "c2x5CCxsdila6"; +# Need the expression in here to force ary[5] to be numeric. This avoids +# test2 failing because ary2 goes str->numeric->str and ary doesn't. +ary = [1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,"abcdef"] +$x = ary.pack($format) +ary2 = $x.unpack($format) + +if ary.length == ary2.length then ok else notok end + +if ary.join(':') == ary2.join(':') then ok else notok end + +if $x =~ /def/ then ok else notok end + +check "math" +if Math.sqrt(4) == 2 + ok +else + notok +end + +include Math +if sqrt(4) == 2 + ok +else + notok +end + +check "struct" +struct_test = Struct.new("Test", :foo, :bar) +if struct_test == Struct::Test + ok +else + notok +end +test = struct_test.new(1, 2) +if test.foo == 1 && test.bar == 2 + ok +else + notok +end +if test[0] == 1 && test[1] == 2 + ok +else + notok +end +a, b = test +if a == 1 && b == 2 + ok +else + notok +end +test[0] = 22 +if test.foo == 22 + ok +else + notok +end +test.bar = 47 +if test.bar == 47 + ok +else + notok +end + +check "variable" +if $$.is_instance_of? Fixnum + ok +else + notok +end + +begin + $$ = 5 + notok +rescue + ok +end + +foobar = "foobar" +$_ = foobar +if $_ == foobar + ok +else + notok +end + +check "trace" +$x = 1234 +$y = 0 +trace_var :$x, proc{$y = $x} +$x = 40414 +if $y == $x + ok +else + notok +end + +untrace_var :$x +$x = 19660208 +if $y != $x + ok +else + notok +end + +trace_var :$x, proc{$x *= 2} +$x = 5 +if $x == 10 + ok +else + notok +end +untrace_var :$x + +check "defined?" +if defined? $x + ok +else + notok +end + +foo=5 +if defined? foo + ok +else + notok +end + +if defined? Array + ok +else + notok +end + +if defined? Object.new + ok +else + notok +end + +if defined? 1 == 2 + ok +else + notok +end + +if defined? fail + ok +else + notok +end + +def defined_test + return defined?(yield) +end + +if defined_test + notok +else + ok +end + +if defined_test{} + ok +else + notok +end + +check "gc" +begin + 1.upto(10000) { + tmp = [0,1,2,3,4,5,6,7,8,9] + } + tmp = nil + ok +rescue + notok +end + +print "end of test\n" if not $failed diff --git a/sample/tkbiff.rb b/sample/tkbiff.rb new file mode 100644 index 0000000000..9b406010cb --- /dev/null +++ b/sample/tkbiff.rb @@ -0,0 +1,121 @@ +#! /usr/local/bin/ruby + +if $ARGV.length == 0 + if ENV['MAIL'] + $spool = ENV['MAIL'] + else + $spool = '/usr/spool/mail/' + ENV['USER'] + end +else + $spool = $ARGV[0] +end + +exit if fork + +require "parsedate" +require "base64" + +include ParseDate + +class Mail + def Mail.new(f) + if !f.is_kind_of?(IO) + f = open(f, "r") + me = super + f.close + else + me = super + end + return me + end + + def initialize(f) + @header = {} + @body = [] + while f.gets() + $_.chop! + continue if /^From / # skip From-line + break if /^$/ # end of header + if /^(\S+):\s*(.*)/ + @header[attr = $1.capitalize] = $2 + elsif attr + sub(/^\s*/, '') + @header[attr] += "\n" + $_ + end + end + + return if ! $_ + + while f.gets() + break if /^From / + @body.push($_) + end + end + + def header + return @header + end + + def body + return @body + end + +end + +require "tkscrollbox" + +$top = TkRoot.new +$top.withdraw +$list = TkScrollbox.new($top) { + relief 'raised' + width 80 + height 8 + setgrid 'yes' + pack +} +TkButton.new($top) { + text 'Dismiss' + command proc {$top.withdraw} + pack('fill'=>'both','expand'=>'yes') +} +$top.bind "Control-c", proc{exit} +$top.bind "Control-q", proc{exit} +$top.bind "space", proc{exit} + +$spool_size = 0 +def check + size = File.size($spool) + if size and size != $spool_size + pop_up if size > 0 + end + Tk.after 5000, proc{check} +end + +def pop_up + outcount = 0; + $spool_size = File.size($spool) + $list.delete 0, 'end' + f = open($spool, "r") + while !f.eof + mail = Mail.new(f) + date, from, subj = mail.header['Date'], mail.header['From'], mail.header['Subject'] + continue if !date + y = m = d = 0 + y, m, d = parsedate(date) if date + from = "sombody@somewhere" if ! from + subj = "(nil)" if ! subj + from = decode_b(from) + subj = decode_b(subj) + $list.insert 'end', format('%-02d/%02d/%02d [%-28.28s] %s',y,m,d,from,subj) + outcount += 1 + end + f.close + if outcount == 0 + $list.insert 'end', "You have no mail." + end + $top.deiconify + Tk.after 2000, proc{$top.withdraw} +end + +check +Tk.mainloop diff --git a/sample/tkbrowse.rb b/sample/tkbrowse.rb new file mode 100644 index 0000000000..dbaa132d1f --- /dev/null +++ b/sample/tkbrowse.rb @@ -0,0 +1,69 @@ +#!/usr/local/bin/ruby +# +# This script generates a directory browser, which lists the working +# directory and allows you to open files or subdirectories by +# double-clicking. + +# Create a scrollbar on the right side of the main window and a listbox +# on the left side. + +require "tkscrollbox" + +list = TkScrollbox.new { + relief 'raised' + width 20 + height 20 + setgrid 'yes' + pack +} + +# The procedure below is invoked to open a browser on a given file; if the +# file is a directory then another instance of this program is invoked; if +# the file is a regular file then the Mx editor is invoked to display +# the file. + +def browse (dir, file) + if dir != "." + file="#{dir}/#{file}" + if File.isdirectory? file + system "browse #{file} &" + else + if File.isfile? file + if ENV['EDITOR'] + system format("%s %s&", ENV['EDITOR'], file) + else + sysmte "xedit #{file}&" + end + else + STDERR.print "\"#{file}\" isn't a directory or regular file" + end + end + end +end + +# Fill the listbox with a list of all the files in the directory (run +# the "ls" command to get that information). + +if $ARGV.length>0 + dir = $ARGV[0] +else + dir="." +end +list.insert 'end', *`ls #{dir}`.split + +# Set up bindings for the browser. + +list.focus +list.bind "Control-q", proc{exit} +list.bind "Control-c", proc{exit} +list.bind "Control-p", proc{ + print "selection <", TkSelection.get, ">\n" +} + +list.bind "Double-Button-1", proc{ + for i in TkSelection.get.split + print "clicked ", i, "\n" + browse dir, i + end +} +Tk.mainloop diff --git a/sample/tkdialog.rb b/sample/tkdialog.rb new file mode 100644 index 0000000000..e83e16d0a8 --- /dev/null +++ b/sample/tkdialog.rb @@ -0,0 +1,62 @@ +#! /usr/local/bin/ruby +require "tk" + +root = TkFrame.new +top = TkFrame.new(root) { + relief 'raised' + border 1 +} +msg = TkMessage.new(top) { + text "File main.c hasn't been saved to disk since \ +it was last modified. What should I do?" + justify 'center' + aspect 200 + font '-Adobe-helvetica-medium-r-normal--*-240*' + pack('padx'=>5, 'pady'=>5, 'expand'=>'yes') +} +top.pack('fill'=>'both') +root.pack + +bot = TkFrame.new(root) { + relief 'raised' + border 1 +} + +TkFrame.new(bot) { |left| + relief 'sunken' + border 1 + pack('side'=>'left', 'expand'=>'yes', 'padx'=>10, 'pady'=> 10) + TkButton.new(left) { + text "Save File" + command "quit 'save'" + pack('expand'=>'yes','padx'=>6,'pady'=> 6) + top.bind "Enter", proc{state 'active'} + msg.bind "Enter", proc{state 'active'} + bot.bind "Enter", proc{state 'active'} + top.bind "Leave", proc{state 'normal'} + msg.bind "Leave", proc{state 'normal'} + bot.bind "Leave", proc{state 'normal'} + Tk.root.bind "ButtonRelease-1", proc{quit 'save'} + Tk.root.bind "Return", proc{quit 'save'} + } +} +TkButton.new(bot) { + text "Quit Anyway" + command "quit 'quit'" + pack('side'=>'left', 'expand'=>'yes', 'padx'=>10) +} +TkButton.new(bot) { + text "Return To Editor" + command "quit 'return'" + pack('side'=>'left', 'expand'=>'yes', 'padx'=>10) +} +bot.pack +root.pack('side'=>'top', 'fill'=>'both', 'expand'=>'yes') + +def quit(button) + print "aaa\n" + print "You pressed the \"#{button}\" button; bye-bye!\n" + exit +end + +Tk.mainloop diff --git a/sample/tkfrom.rb b/sample/tkfrom.rb new file mode 100644 index 0000000000..4a0d8c2b5d --- /dev/null +++ b/sample/tkfrom.rb @@ -0,0 +1,115 @@ +#! /usr/local/bin/ruby + +require "parsedate" +require "base64" + +include ParseDate + +class Mail + def Mail.new(f) + if !f.is_kind_of?(IO) + f = open(f, "r") + me = super + f.close + else + me = super + end + return me + end + + def initialize(f) + @header = {} + @body = [] + while f.gets() + $_.chop! + continue if /^From / # skip From-line + break if /^$/ # end of header + if /^(\S+):\s*(.*)/ + @header[attr = $1.capitalize] = $2 + elsif attr + sub(/^\s*/, '') + @header[attr] += "\n" + $_ + end + end + + return if ! $_ + + while f.gets() + break if /^From / + @body.push($_) + end + end + + def header + return @header + end + + def body + return @body + end + +end + +$ARGV[0] = '/usr/spool/mail/' + ENV['USER'] if $ARGV.length == 0 + +require "tk" +list = scroll = nil +TkFrame.new{|f| + list = TkListbox.new(f) { + yscroll proc{|idx| + scroll.set *idx + } + relief 'raised' +# geometry "80x5" + width 80 + height 5 + setgrid 'yes' + pack('side'=>'left','fill'=>'both','expand'=>'yes') + } + scroll = TkScrollbar.new(f) { + command proc{|idx| + list.yview *idx + } + pack('side'=>'right','fill'=>'y') + } + pack +} +root = Tk.root +TkButton.new(root) { + text 'Dismiss' + command proc {exit} + pack('fill'=>'both','expand'=>'yes') +} +root.bind "Control-c", proc{exit} +root.bind "Control-q", proc{exit} +root.bind "space", proc{exit} + +$outcount = 0; +for file in $ARGV + continue if !File.exists?(file) + f = open(file, "r") + while !f.eof + mail = Mail.new(f) + date, from, subj = mail.header['Date'], mail.header['From'], mail.header['Subject'] + continue if !date + y = m = d = 0 + y, m, d = parsedate(date) if date + from = "sombody@somewhere" if ! from + subj = "(nil)" if ! subj + from = decode_b(from) + subj = decode_b(subj) + list.insert 'end', format('%-02d/%02d/%02d [%-28.28s] %s',y,m,d,from,subj) + $outcount += 1 + end + f.close +end + +limit = 10000 +if $outcount == 0 + list.insert 'end', "You have no mail." + limit = 2000 +end +Tk.after limit, proc{ + exit +} +Tk.mainloop diff --git a/sample/tkhello.rb b/sample/tkhello.rb new file mode 100644 index 0000000000..1ff1403e71 --- /dev/null +++ b/sample/tkhello.rb @@ -0,0 +1,13 @@ +require "tk" + +TkButton.new { + text 'hello' + command proc{print "hello\n"} + pack('fill'=>'x') +} +TkButton.new { + text 'quit' + command 'exit' + pack('fill'=>'x') +} +Tk.mainloop diff --git a/sample/tkline.rb b/sample/tkline.rb new file mode 100644 index 0000000000..843893b5d9 --- /dev/null +++ b/sample/tkline.rb @@ -0,0 +1,29 @@ +require "tkclass" + +$c = Canvas.new +$c.pack +$start_x = start_y = 0 + +def do_press(x, y) + $start_x = x + $start_y = y + $current_line = Line.new($c, x, y, x, y, 'fill' => 'gray') +end +def do_motion(x, y) + if $current_line + $current_line.coords $start_x, $start_y, x, y + end +end + +def do_release(x, y) + if $current_line + $current_line.coords $start_x, $start_y, x, y + $current_line.fill 'black' + $current_line = nil + end +end + +$c.bind("1", proc{|e| do_press e.x,e.y}) +$c.bind("B1-Motion", proc{|e| do_motion e.x,e.y}) +$c.bind("ButtonRelease-1", proc{|e| do_release e.x,e.y}) +Tk.mainloop diff --git a/sample/tktimer.rb b/sample/tktimer.rb new file mode 100644 index 0000000000..b8b3617646 --- /dev/null +++ b/sample/tktimer.rb @@ -0,0 +1,49 @@ +#!/usr/local/bin/ruby +# This script generates a counter with start and stop buttons. + +require "tk" +$label = TkLabel.new { + text '0.00' + relief 'raised' + width 10 + pack('side'=>'bottom', 'fill'=>'both') +} + +TkButton.new { + text 'Start' + command proc { + if $stopped + $stopped = FALSE + tick + end + } + pack('side'=>'left','fill'=>'both','expand'=>'yes') +} +TkButton.new { + text 'Stop' + command proc{ + $stopped = TRUE + } + pack('side'=>'right','fill'=>'both','expand'=>'yes') +} + +$seconds=0 +$hundredths=0 +$stopped=TRUE + +def tick + if $stopped then return end + Tk.after 50, proc{tick} + $hundredths+=5 + if $hundredths >= 100 + $hundredths=0 + $seconds+=1 + end + $label.text format("%d.%02d", $seconds, $hundredths) +end + +root = Tk.root +root.bind "Control-c", proc{root.destroy} +root.bind "Control-q", proc{root.destroy} +Tk.root.focus +Tk.mainloop diff --git a/sample/trap.pl b/sample/trap.pl deleted file mode 100644 index ce022d4062..0000000000 --- a/sample/trap.pl +++ /dev/null @@ -1,6 +0,0 @@ -$SIG{'INT'} = 'test'; - -while (<>) { - print; -} -sub test { print "C-c handled\n"; } diff --git a/sample/trap.rb b/sample/trap.rb deleted file mode 100644 index e552a0fddc..0000000000 --- a/sample/trap.rb +++ /dev/null @@ -1,3 +0,0 @@ -trap('print("C-c handled\n")', 'INT', 'HUP') -print("---\n") -while gets(); print($_) end diff --git a/sample/tt.rb b/sample/tt.rb deleted file mode 100644 index f4960feaea..0000000000 --- a/sample/tt.rb +++ /dev/null @@ -1,100 +0,0 @@ -module Print - print("in Print\n") - def println(*args) - for a in args - print(a) - end - print("\n") - end - - def println2(*args) - print(*args) - print("\n") - end -end - -module Print2 - def println(*args) - print("pr2: "); - super - end -end - -module Print3 - include Print2 - def println(*args) - print("pr3: "); - super - end -end - -include Print, Print2, Print3 - -println2("in TopLevel") - -print("a: ", $OPT_test, "\n") -printf("%10.5g: %*s -> 0x%x\n", 123345, -10, Print, Print.id); - -println("a+ matches aaa at ", "bccAAaaa" =~ /a+/) -ttt = "this is a ���� ����" -if offset = (ttt =~ /this ([^ ]*) (.*)/) - println("0 = ", $&); - println("1 = ", $1); - println("2 = ", $2); -end - -class Fib : Object - print("in Fib:Object\n") - - def Fib.test(*args) - println("in Fib.test") - - if args; println(*args) end - args = args.grep(/^c/) - super(*args) - end - - def init - println("in Fib.init"); - end - - def fib(n) - a =0; b = 1 - - while b <= n - c = a; a = b; b = c+b - end - return b - end -end - -def Object.test(*args) - println("in Object.test") - if args; println(*args) end -end - -Fib.test("abc", "def", "aaa", "ccc") -println("1:", 0x3fffffffa) -println("2:", 0x3ffffffa) -#println("3:", 0x40000000+0x40000000) - -fib = Fib.new - -fib.init -print(Fib, ":") - -#for i in 1 .. 100 -# fib.fib(90000) -#end - -println(fib.fib(9000)) - -def tt - for i in 1..10 - println("i:", i); - yield(i); - end -end - -test = tt() {|i|break if i == 2} -println([1,2,3,4].join(":")) |