diff options
Diffstat (limited to 'sample')
-rw-r--r-- | sample/clnt.rb | 6 | ||||
-rw-r--r-- | sample/dir.rb | 4 | ||||
-rw-r--r-- | sample/eval.rb | 41 | ||||
-rw-r--r-- | sample/evaldef.rb | 26 | ||||
-rw-r--r-- | sample/export.rb | 2 | ||||
-rw-r--r-- | sample/fact.rb | 8 | ||||
-rwxr-xr-x | sample/from.rb | 14 | ||||
-rw-r--r-- | sample/fullpath.pl | 22 | ||||
-rw-r--r-- | sample/fullpath.rb | 6 | ||||
-rwxr-xr-x | sample/getopts.test | 25 | ||||
-rw-r--r-- | sample/io.rb | 4 | ||||
-rwxr-xr-x | sample/less.rb | 4 | ||||
-rw-r--r-- | sample/list.rb | 14 | ||||
-rwxr-xr-x | sample/mpart.rb | 6 | ||||
-rw-r--r-- | sample/observ.rb | 31 | ||||
-rw-r--r-- | sample/philos.rb | 54 | ||||
-rw-r--r-- | sample/pi.rb | 18 | ||||
-rw-r--r-- | sample/rcs.rb | 2 | ||||
-rw-r--r-- | sample/regx.rb | 23 | ||||
-rw-r--r-- | sample/ruby-mode.el | 575 | ||||
-rw-r--r-- | sample/sieve.rb | 2 | ||||
-rw-r--r-- | sample/svr.rb | 2 | ||||
-rw-r--r-- | sample/test.rb | 981 | ||||
-rwxr-xr-x | sample/time.rb | 2 | ||||
-rw-r--r-- | sample/tkbiff.rb | 46 | ||||
-rw-r--r-- | sample/tkbrowse.rb | 8 | ||||
-rw-r--r-- | sample/tkfrom.rb | 13 | ||||
-rw-r--r-- | sample/tkline.rb | 25 | ||||
-rw-r--r-- | sample/trojan.pl | 12 | ||||
-rw-r--r-- | sample/tsvr.rb | 23 | ||||
-rwxr-xr-x | sample/uumerge.rb | 8 |
31 files changed, 1072 insertions, 935 deletions
diff --git a/sample/clnt.rb b/sample/clnt.rb index c8c4b2db9f..7998379aa2 100644 --- a/sample/clnt.rb +++ b/sample/clnt.rb @@ -3,15 +3,15 @@ require "socket" -host=(if $ARGV.length == 2; $ARGV.shift; else "localhost"; end) +host=(if ARGV.length == 2; ARGV.shift; else "localhost"; end) print("Trying ", host, " ...") STDOUT.flush -s = TCPsocket.open(host, $ARGV.shift) +s = TCPsocket.open(host, ARGV.shift) print(" done\n") print("addr: ", s.addr.join(":"), "\n") print("peer: ", s.peeraddr.join(":"), "\n") while gets() s.write($_) - print(s.gets) + print(s.readline) end s.close diff --git a/sample/dir.rb b/sample/dir.rb index 3349dc7b6d..1fc0bb2aa8 100644 --- a/sample/dir.rb +++ b/sample/dir.rb @@ -1,9 +1,9 @@ # directory access # list all files but .*/*~/*.o dirp = Dir.open(".") -dirp.rewind for f in dirp - if !(~/^\./ || ~/~$/ || ~/\.o/) + $_ = f + if (~/^\./ || ~/~$/ || ~/\.o/) print f, "\n" end end diff --git a/sample/eval.rb b/sample/eval.rb new file mode 100644 index 0000000000..da31b77153 --- /dev/null +++ b/sample/eval.rb @@ -0,0 +1,41 @@ +line = '' +indent=0 +print "ruby> " +while TRUE + l = gets + if not l + break if line == '' + else + line = line + l + if l =~ /,\s*$/ + print "ruby| " + next + end + if l =~ /^\s*(class|module|def|if|case|while|for|begin)\b[^_]/ + indent += 1 + end + if l =~ /^\s*end\b[^_]/ + indent -= 1 + end + if l =~ /{\s*(\|.*\|)?\s*$/ + indent += 1 + end + if l =~ /^\s*\}/ + indent -= 1 + end + if indent > 0 + print "ruby| " + next + end + end + begin + print eval(line).inspect, "\n" + rescue + $! = 'exception raised' if not $! + print "ERR: ", $!, "\n" + end + break if not l + line = '' + print "ruby> " +end +print "\n" diff --git a/sample/evaldef.rb b/sample/evaldef.rb deleted file mode 100644 index 2cedd54999..0000000000 --- a/sample/evaldef.rb +++ /dev/null @@ -1,26 +0,0 @@ -# method definition by eval() -# output: -# bar -# (eval):26: method `baz' not available for "#<foo: 0xbfc5c>"(foo) - -class Foo - def foo - eval(" -def baz - print(\"bar\n\") -end") - end -end - -class Bar : Foo - def bar - baz() - end -end - -f = Foo.new -b = Bar.new - -b.foo -b.bar -f.baz diff --git a/sample/export.rb b/sample/export.rb index 2d05d8afd7..750b5c1948 100644 --- a/sample/export.rb +++ b/sample/export.rb @@ -28,7 +28,7 @@ f.printf "%s\n", Foo f.quux -class Bar : Foo +class Bar<Foo def quux super baz() diff --git a/sample/fact.rb b/sample/fact.rb new file mode 100644 index 0000000000..49678bc9d0 --- /dev/null +++ b/sample/fact.rb @@ -0,0 +1,8 @@ +def fact(n) + if n == 0 + 1 + else + n * fact(n-1) + end +end +print fact(ARGV[0].to_i), "\n" diff --git a/sample/from.rb b/sample/from.rb index 2f5fcebe12..2ef000face 100755 --- a/sample/from.rb +++ b/sample/from.rb @@ -5,15 +5,15 @@ require "base64" include ParseDate -if $ARGV[0] == '-w' +if ARGV[0] == '-w' wait = TRUE - $ARGV.shift + ARGV.shift end class Mail def Mail.new(f) - if !f.is_kind_of?(IO) + if !f.kind_of?(IO) f = open(f, "r") me = super f.close @@ -28,7 +28,7 @@ class Mail @body = [] while f.gets() $_.chop! - continue if /^From / # skip From-line + next if /^From / # skip From-line break if /^$/ # end of header if /^(\S+):\s*(.*)/ @header[attr = $1.capitalize] = $2 @@ -56,7 +56,7 @@ class Mail end -$ARGV[0] = '/usr/spool/mail/' + ENV['USER'] if $ARGV.length == 0 +ARGV[0] = '/usr/spool/mail/' + ENV['USER'] if ARGV.length == 0 $outcount = 0; def fromout(date, from, subj) @@ -72,8 +72,8 @@ def fromout(date, from, subj) $outcount += 1 end -for file in $ARGV - continue if !File.exists?(file) +for file in ARGV + next if !File.exist?(file) f = open(file, "r") while !f.eof mail = Mail.new(f) diff --git a/sample/fullpath.pl b/sample/fullpath.pl deleted file mode 100644 index a07b90edd4..0000000000 --- a/sample/fullpath.pl +++ /dev/null @@ -1,22 +0,0 @@ -#! /usr/local/bin/perl -# convert ls-lR filename into fullpath. - -$path = shift; -if (!defined $path) { - $path = ""; -} -elsif ($path !~ /\/$/) { - $path .= "/" -} - -while (<>) { - if (/:$/) { - chop; chop; - $path = $_ . "/"; - } elsif (/^total/ || /^d/) { - next; - } elsif (/^(.*\d )(.+)$/) { - print $1, $path, $2, "\n"; - } -} - diff --git a/sample/fullpath.rb b/sample/fullpath.rb index 6c528f6f96..ce268e20b9 100644 --- a/sample/fullpath.rb +++ b/sample/fullpath.rb @@ -1,9 +1,9 @@ #! /usr/local/bin/ruby # convert ls-lR filename into fullpath. -if $ARGV[0] =~ /-p/ - $ARGV.shift - path = $ARGV.shift +if ARGV[0] =~ /-p/ + ARGV.shift + path = ARGV.shift end if path == nil diff --git a/sample/getopts.test b/sample/getopts.test index adef7628db..2866bccea8 100755 --- a/sample/getopts.test +++ b/sample/getopts.test @@ -3,13 +3,16 @@ load("parsearg.rb") def usage() - printf("Usage:\n") - printf("This is Getopt test program \n") + printf "Usage:\n" + printf "%s -d [-x x] [-y y] [--geometry geom] [--version] [string ...]\n", $0 end $USAGE = 'usage' -parseArgs(0, !nil, "d", "x:", "y:", "version", "geometry:") +parseArgs(0, "d&(x|y)", "dfg", "x:", "y:", "geometry:800x600", "version") if ($OPT_d) + if $OPT_version + printf "version 1.0\n" + end if ($OPT_x) printf("x = %d\n", $OPT_x.to_i) end @@ -19,13 +22,15 @@ if ($OPT_d) if ($OPT_geometry) printf("geometry = %s\n", $OPT_geometry) end + if $OPT_f + printf "f = TRUE\n" + end + if $OPT_g + printf "g = TRUE\n" + end end -if ($OPT_version) - printf("version 1.00\n") -end - -while ($ARGV.length != 0) - print ("other = ", $ARGV[0], "\n") - $ARGV.shift +while (ARGV.length != 0) + print "other = ", ARGV[0], "\n" + ARGV.shift end diff --git a/sample/io.rb b/sample/io.rb index c12e4f4498..0b38d2112d 100644 --- a/sample/io.rb +++ b/sample/io.rb @@ -15,8 +15,8 @@ for i in "abc\n\ndef\nghi\n" print("tt: ", i) end -printf("%s:(%d)%s\n", $0, $ARGV.length, $ARGV[0]) -passwd = open($ARGV[0], "r") +printf("%s:(%d)%s\n", $0, ARGV.length, ARGV[0]) +passwd = open(ARGV[0], "r") #printf("%s", passwd.find{i|i =~ /\*/}) n = 1 diff --git a/sample/less.rb b/sample/less.rb index b0906d5d22..8be359108f 100755 --- a/sample/less.rb +++ b/sample/less.rb @@ -3,8 +3,8 @@ ZCAT = "/usr/local/bin/zcat" LESS = "/usr/local/bin/less" -FILE = $ARGV.pop -OPTION = (if $ARGV.length == 0; "" else $ARGV.join(" "); end) +FILE = ARGV.pop +OPTION = (if ARGV.length == 0; "" else ARGV.join(" "); end) if FILE =~ /\.(Z|gz)$/ exec(format("%s %s | %s %s", ZCAT, FILE, LESS, OPTION)) diff --git a/sample/list.rb b/sample/list.rb index 93e3182f84..76035e67d6 100644 --- a/sample/list.rb +++ b/sample/list.rb @@ -4,20 +4,20 @@ class MyElem def initialize(item) # @変数はインスタンス変数(宣言は要らない) @data = item - @next = nil + @succ = nil end def data @data end - def next - @next + def succ + @succ end # 「obj.data = val」としたときに暗黙に呼ばれるメソッド - def next=(new) - @next = new + def succ=(new) + @succ = new end end @@ -25,7 +25,7 @@ class MyList def add_to_list(obj) elt = MyElem.new(obj) if @head - @tail.next = elt + @tail.succ = elt else @head = elt end @@ -36,7 +36,7 @@ class MyList elt = @head while elt yield elt - elt = elt.next + elt = elt.succ end end diff --git a/sample/mpart.rb b/sample/mpart.rb index 2374ae0938..6c40d50e18 100755 --- a/sample/mpart.rb +++ b/sample/mpart.rb @@ -4,12 +4,12 @@ lines = 1000 -if ($ARGV[0] =~ /^-(\d+)$/ ) +if (ARGV[0] =~ /^-(\d+)$/ ) lines = $1.to_i; - $ARGV.shift; + ARGV.shift; end -basename = $ARGV[0] +basename = ARGV[0] extname = "part" part = 1 diff --git a/sample/observ.rb b/sample/observ.rb new file mode 100644 index 0000000000..f7b1e73137 --- /dev/null +++ b/sample/observ.rb @@ -0,0 +1,31 @@ +#! /usr/local/bin/ruby + +require "thread" +require "observer" + +class Tick + include Observable + def initialize + Thread.start do + while TRUE + sleep 0.999 + changed + notify_observers(Time.now.strftime("%H:%M:%S")) + end + end + end +end + +class Clock + def initialize + @tick = Tick.new + @tick.add_observer(self) + end + def update(time) + print "\e[8D", time + STDOUT.flush + end +end + +clock = Clock.new +sleep diff --git a/sample/philos.rb b/sample/philos.rb new file mode 100644 index 0000000000..ee0a8cd5fc --- /dev/null +++ b/sample/philos.rb @@ -0,0 +1,54 @@ +# +# The Dining Philosophers - thread example +# +require "thread" + +srand +#srand +N=9 # number of philosophers +$forks = [] +for i in 0..N-1 + $forks[i] = Mutex.new +end +$state = "-o"*N + +def wait + sleep rand(20)/10.0 +end + +def think(n) + wait +end + +def eat(n) + wait +end + +def philosopher(n) + while TRUE + think n + $forks[n].lock + if not $forks[(n+1)%N].try_lock + $forks[n].unlock # avoid deadlock + continue + end + $state[n*2] = ?|; + $state[(n+1)%N*2] = ?|; + $state[n*2+1] = ?*; + print $state, "\n" + eat(n) + $state[n*2] = ?-; + $state[(n+1)%N*2] = ?-; + $state[n*2+1] = ?o; + print $state, "\n" + $forks[n].unlock + $forks[(n+1)%N].unlock + end +end + +for i in 0..N-1 + Thread.start{philosopher(i)} + sleep 0.1 +end + +sleep diff --git a/sample/pi.rb b/sample/pi.rb new file mode 100644 index 0000000000..49067cc347 --- /dev/null +++ b/sample/pi.rb @@ -0,0 +1,18 @@ +#!/usr/local/bin/ruby + +k, a, b, a1, b1 = 2, 4, 1, 12, 4 + +while TRUE + # Next approximation + p, q, k = k*k, 2*k+1, k+1 + a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1 + # Print common digits + d = a / b + d1 = a1 / b1 + while d == d1 + print d + $stdout.flush + a, a1 = 10*(a%b), 10*(a1%b1) + d, d1 = a/b, a1/b1 + end +end diff --git a/sample/rcs.rb b/sample/rcs.rb index 13476267b2..3f74da9ef2 100644 --- a/sample/rcs.rb +++ b/sample/rcs.rb @@ -7,7 +7,7 @@ hdw = dw / 2.0 w = 20.0 # h =1.0 # d = 0.2 # P -ss="abcdefghijklmnopqrstuvwxyz0123456789!#$%^&*()-=\\[];'`,./" +ss="abcdefghijklmnopqrstuvwxyz0123456789#!$%^&*()-=\\[];'`,./" rnd = srand() while gets() diff --git a/sample/regx.rb b/sample/regx.rb new file mode 100644 index 0000000000..b9d8ca6e14 --- /dev/null +++ b/sample/regx.rb @@ -0,0 +1,23 @@ +st = "\033[7m" +en = "\033[m" +#st = "<<" +#en = ">>" + +while TRUE + print "str> " + STDOUT.flush + input = gets + break if not input + if input != "" + str = input + str.chop! + end + print "pat> " + STDOUT.flush + re = gets + break if not re + re.chop! + str.gsub! re, "#{st}&#{en}" + print str, "\n" +end +print "\n" diff --git a/sample/ruby-mode.el b/sample/ruby-mode.el index b555994fea..9dfde8588c 100644 --- a/sample/ruby-mode.el +++ b/sample/ruby-mode.el @@ -7,8 +7,18 @@ ;;; created at: Fri Feb 4 14:49:13 JST 1994 ;;; +(defconst ruby-mode-version "1.0.2") + (defconst ruby-block-beg-re - "class\\|module\\|def\\|if\\|case\\|while\\|for\\|begin" + "class\\|module\\|def\\|if\\|unless\\|case\\|while\\|until\\|for\\|begin\\|do" + ) + +(defconst ruby-indent-beg-re + "\\(\\s *\\(class\\|module\\|def\\)\\)\\|if\\|unless\\|case\\|while\\|until\\|for\\|begin" + ) + +(defconst ruby-modifier-re + "if\\|unless\\|while\\|until" ) (defconst ruby-block-mid-re @@ -18,13 +28,13 @@ (defconst ruby-block-end-re "end") (defconst ruby-delimiter - (concat "[?$/(){}#\"'`]\\|\\[\\|\\]\\|\\<\\(" - ruby-block-beg-re "\\|" ruby-block-end-re "\\)\\>") + (concat "[?$/%(){}#\"'`]\\|\\[\\|\\]\\|\\<\\(" + ruby-block-beg-re "\\|" ruby-block-end-re "\\)\\>") ) (defconst ruby-negative - (concat "^[ \t]*\\(\\b\\(" ruby-block-mid-re "\\)\\|\\(" - ruby-block-end-re "\\)\\>\\|\\}\\|\\]\\)") + (concat "^[ \t]*\\(\\(" ruby-block-mid-re "\\)\\|\\(" + ruby-block-end-re "\\)\\>\\|\\}\\|\\]\\)") ) (defconst ruby-operator-chars "[,.+*/%-&|^~=<>:]") @@ -44,6 +54,10 @@ (define-key ruby-mode-map "}" 'ruby-electric-brace) (define-key ruby-mode-map "\e\C-a" 'ruby-beginning-of-defun) (define-key ruby-mode-map "\e\C-e" 'ruby-end-of-defun) + (define-key ruby-mode-map "\e\C-b" 'ruby-beginning-of-block) + (define-key ruby-mode-map "\e\C-f" 'ruby-end-of-block) + (define-key ruby-mode-map "\e\C-p" 'ruby-beginning-of-block) + (define-key ruby-mode-map "\e\C-n" 'ruby-end-of-block) (define-key ruby-mode-map "\t" 'ruby-indent-command) (define-key ruby-mode-map "\C-m" 'ruby-reindent-then-newline-and-indent) (define-key ruby-mode-map "\C-j" 'newline)) @@ -60,7 +74,7 @@ (modify-syntax-entry ?\n ">" 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) @@ -121,17 +135,6 @@ The variable ruby-indent-level controls the amount of indentation. (back-to-indentation) (current-column))) -(defun ruby-delete-indentation () - (let - ((b nil) - (m nil)) - (save-excursion - (beginning-of-line) - (setq b (point)) - (back-to-indentation) - (setq m (point))) - (delete-region b m))) - (defun ruby-indent-line (&optional flag) "Correct indentation of the current ruby line." (ruby-indent-to (ruby-calculate-indent))) @@ -141,222 +144,294 @@ The variable ruby-indent-level controls the amount of indentation. (ruby-indent-line t)) (defun ruby-indent-to (x) - (let ((p nil) beg end) - (if (null x) - nil - (setq p (- (current-column) (ruby-current-indentation))) - (ruby-delete-indentation) - (beginning-of-line) - (save-excursion + (if x + (let (shift top beg) + (and (< x 0) + (error "invalid nest")) + (setq shift (current-column)) + (beginning-of-line) (setq beg (point)) - (forward-line 1) - (setq end (point))) - (indent-to x) - (if (> p 0) (forward-char p))))) + (back-to-indentation) + (setq top (current-column)) + (skip-chars-backward " \t") + (cond + ((>= x shift) + (setq shift 0)) + ((>= shift top) + (setq shift (- shift top))) + (t (setq shift 0))) + (if (and (bolp) + (= x top)) + (move-to-column (+ x shift)) + (move-to-column top) + (delete-region beg (point)) + (beginning-of-line) + (indent-to x) + (move-to-column (+ x shift)))))) (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)))))) + (if (looking-at "\\?") + (progn + (or (bolp) (forward-char -1)) + (not (looking-at "\\sw"))) + (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) - (in-string nil) - (in-paren nil) - (depth 0) - (nest nil)) + (indent 0) + (in-string nil) + (in-paren nil) + (depth 0) + (nest nil) + (pcol nil)) (save-excursion - (if start - (goto-char start) - (ruby-beginning-of-defun)) - (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)))) - ((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 + (if start + (goto-char start) + (ruby-beginning-of-indent)) + (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 - ((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)) - (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 + ((or (looking-at "\"") ;skip string + (looking-at "'") + (looking-at "`")) + (setq w (char-after (point))) + (cond + ((and (not (eobp)) + (re-search-forward (format "[^\\]%c" w) indent-point t)) + nil) + (t + (setq in-string (point)) + (goto-char indent-point)))) + ((looking-at "/") + (cond + ((and (not (eobp)) (ruby-expr-beg)) + (if (re-search-forward "[^\\]/" indent-point t) + nil + (setq in-string (point)) + (goto-char indent-point))) + (t + (goto-char pnt)))) + ((looking-at "%") + (cond + ((and (not (eobp)) (ruby-expr-beg) + (looking-at "%[Qq\"'Rr/Xx`]\\(.\\)")) + (setq w (buffer-substring (match-beginning 1) + (match-end 1))) + (cond + ((string= w "[") (setq w "]")) + ((string= w "{") (setq w "}")) + ((string= w "(") (setq w ")")) + ((string= w "<") (setq w ">"))) + (goto-char (match-end 0)) + (if (search-forward w indent-point t) + nil + (setq in-string (point)) + (goto-char indent-point))) + (t + (goto-char pnt)))) + ((looking-at "\\?") ;skip ?char + (cond + ((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 (point)) + ) + ((looking-at "(") + (setq nest (cons (cons (char-after (point)) pnt) nest)) + (setq pcol (cons (cons pnt depth) pcol)) + (setq depth 0) + (goto-char pnt) + ) + ((looking-at "[\\[{]") + (setq nest (cons (cons (char-after (point)) pnt) nest)) + (setq depth (1+ depth)) + (goto-char pnt) + ) + ((looking-at ")") (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)))) + (setq depth (cdr (car pcol))) + (setq pcol (cdr pcol)) + (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) + (setq w (char-after (point))) + (and (not (eq ?_ w)) + (not (eq ?! w)) + (not (eq ?? w)))) + (progn + (goto-char (match-beginning 0)) + (if (looking-at ruby-modifier-re) + (ruby-expr-beg) + t)) + (progn + (setq nest (cons (cons nil pnt) nest)) + (setq depth (1+ depth)))) + (if (looking-at "def\\s *[/`]") + (goto-char (match-end 0)) + (goto-char pnt))) + (t + (error (format "bad string %s" + (buffer-substring (point) pnt) + ))))))) + (list in-string (car nest) depth (car (car pcol)))))) (defun ruby-calculate-indent (&optional parse-start) (save-excursion (beginning-of-line) (let ((indent-point (point)) - (case-fold-search nil) - state bol eol - (indent 0)) - (if parse-start - (goto-char parse-start) - (ruby-beginning-of-defun) - (setq parse-start (point))) - (setq state (ruby-parse-region parse-start indent-point)) - (cond - ((nth 0 state) ; within string - (setq indent nil)) ; do nothing - - ((nth 1 state) ; in paren - (goto-char (cdr (nth 1 state))) - (setq indent - (if (and (eq (car (nth 1 state)) ?\( ) - (not (looking-at "(\\s *$"))) - (current-column) - (+ (current-indentation) ruby-indent-level)))) - - ((> (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))) - - (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)) + (case-fold-search nil) + state bol eol + (indent 0)) + (if parse-start + (goto-char parse-start) + (ruby-beginning-of-indent) + (setq parse-start (point))) + (back-to-indentation) + (setq indent (current-column)) + (setq state (ruby-parse-region parse-start indent-point)) + (cond + ((nth 0 state) ; within string + (setq indent nil)) ; do nothing + + ((car (nth 1 state)) ; in paren + (goto-char (cdr (nth 1 state))) + (if (eq (car (nth 1 state)) ?\( ) + (let ((column (current-column)) + (s (ruby-parse-region (point) indent-point))) + (cond + ((> (nth 2 s) 0) + (goto-char (cdr (nth 1 s))) + (forward-word -1) + (setq indent (+ (current-column) ruby-indent-level))) + (t + (setq indent (current-column))))) + (cond + ((nth 3 state) + (goto-char (nth 3 state)) + (setq indent (+ (current-column) ruby-indent-level))) + (t + (goto-char parse-start) + (back-to-indentation) + (setq indent (+ (current-column) (* (nth 2 state) ruby-indent-level))))) + )) + + ((> (nth 2 state) 0) ; in nest + (goto-char (cdr (nth 1 state))) + (forward-word -1) ; skip back a keyword + (cond + ((looking-at "do") ; iter block is a special case + (cond + ((nth 3 state) + (goto-char (nth 3 state)) + (setq indent (+ (current-column) ruby-indent-level))) + (t + (goto-char parse-start) + (back-to-indentation) + (setq indent (+ (current-column) (* (nth 2 state) ruby-indent-level)))))) + (t + (setq indent (+ (current-column) ruby-indent-level))))) + + ((< (nth 2 state) 0) ; in negative nest + (setq indent (+ (current-column) (* (nth 2 state) ruby-indent-level))))) + + (cond + (indent + (goto-char indent-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))) - + (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) + (skip-chars-backward " \t") + (or (bobp) (forward-char -1)) + (and (looking-at ruby-operator-chars) + (or (not (or (eq ?/ (char-after (point))))) + (null (nth 0 (ruby-parse-region parse-start (point))))) + (save-excursion + (goto-char parse-start)) + (not (eq (char-after (1- (point))) ?$)) + (or (not (eq ?| (char-after (point)))) + (save-excursion + (or (eolp) (forward-char -1)) + (and (search-backward "|") + (skip-chars-backward " \t\n") + (and (not (eolp)) + (progn + (forward-char -1) + (not (looking-at "\\{"))) + (progn + (forward-word -1) + (not (looking-at "do\\>[^_]"))))))) + (setq indent (+ indent ruby-indent-level))))))) + indent))) + (defun ruby-electric-brace (arg) (interactive "P") (self-insert-command (prefix-numeric-value arg)) (ruby-indent-line t)) (defun ruby-beginning-of-defun (&optional arg) - "Move backward to next beginning-of-defun. + "Move backward to next beginning-of-defun. With argument, do this that many times. Returns t unless search stops due to end of buffer." (interactive "p") @@ -364,6 +439,13 @@ Returns t unless search stops due to end of buffer." nil 'move (or arg 1)) (progn (beginning-of-line) t))) +(defun ruby-beginning-of-indent () + (and (re-search-backward (concat "^\\(" ruby-indent-beg-re "\\)\\b") + nil 'move) + (progn + (beginning-of-line) + t))) + (defun ruby-end-of-defun (&optional arg) "Move forward to next end of defun. An end of a defun is found by moving forward from the beginning of one." @@ -373,6 +455,42 @@ An end of a defun is found by moving forward from the beginning of one." (progn (beginning-of-line) t)) (forward-line 1)) +(defun ruby-move-to-block (n) + (let (start pos done down) + (setq start (ruby-calculate-indent)) + (if (eobp) + nil + (while (and (not (bobp)) (not done)) + (forward-line n) + (cond + ((looking-at "^$")) + ((looking-at "^\\s *#")) + (t + (setq pos (current-indentation)) + (cond + ((< start pos) + (setq down t)) + ((and down (= pos start)) + (setq done t)) + ((> start pos) + (setq done t))))) + (if done + (progn + (back-to-indentation) + (if (looking-at ruby-block-mid-re) + (setq done nil))))))) + (back-to-indentation)) + +(defun ruby-beginning-of-block () + "Move backward to next beginning-of-block" + (interactive) + (ruby-move-to-block -1)) + +(defun ruby-end-of-block () + "Move forward to next beginning-of-block" + (interactive) + (ruby-move-to-block 1)) + (defun ruby-reindent-then-newline-and-indent () (interactive "*") (save-excursion @@ -383,39 +501,32 @@ An end of a defun is found by moving forward from the beginning of one." (indent-according-to-mode)) (indent-according-to-mode)) -(defun ruby-encomment-region (beg end) - (interactive "r") - (save-excursion - (goto-char beg) - (while (re-search-forward "^" end t) - (replace-match "#" nil nil)))) +(fset 'ruby-encomment-region (symbol-function 'comment-region)) (defun ruby-decomment-region (beg end) (interactive "r") (save-excursion (goto-char beg) (while (re-search-forward "^\\([ \t]*\\)#" end t) - (replace-match "\\1" nil nil)))) + (replace-match "\\1" nil nil) + (save-excursion + (ruby-indent-line))))) (if (featurep 'hilit19) (hilit-set-mode-patterns 'ruby-mode - '(("\\s #.*$" nil comment) - ("^#.*$" nil comment) - ("\\$\\(.\\|\\sw+\\)" nil type) - ("[^$\\?]\\(\"[^\\\"]*\\(\\\\\\(.\\|\n\\)[^\\\"]*\\)*\"\\)" 1 string) + '(("[^$\\?]\\(\"[^\\\"]*\\(\\\\\\(.\\|\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) + ("[^$\\?]\\(`[^\\`]*\\(\\\\\\(.\\|\n\\)[^\\`]*\\)*`\\)" 1 string) + ("^\\s *#.*$" nil comment) + ("[^$@?\\]\\(#[^$@{].*$\\)" 1 comment) + ("[^a-zA-Z_]\\(\\?\\(\\\\[CM]-\\)*.\\)" 1 string) + ("^\\s *\\(require\\|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) + ("[^_]\\<\\(begin\\|case\\|else\\|elsif\\|end\\|ensure\\|for\\|if\\|unless\\|rescue\\|then\\|when\\|while\\|until\\|do\\)\\>[^_]" 1 defun) + ("[^_]\\<\\(and\\|break\\|next\\|raise\\|fail\\|in\\|not\\|or\\|redo\\|retry\\|return\\|super\\|yield\\)\\>[^_]" 1 keyword) + ("[^_]\\<\\(self\\|nil\\|[A-Z][a-zA-Z_0-9]*\\)\\>[^_]" 1 define) + ("\\$\\(.\\|\\sw+\\)" nil type) + ("[$@].[a-zA-Z_0-9]*" nil struct) ("^__END__" nil label)))) diff --git a/sample/sieve.rb b/sample/sieve.rb index a953784284..03ff8a67f4 100644 --- a/sample/sieve.rb +++ b/sample/sieve.rb @@ -1,6 +1,6 @@ # sieve of Eratosthenes sieve = [] -if ! max = $ARGV.shift; max = 100; end +if ! max = ARGV.shift; max = 100; end max = max.to_i print "1" diff --git a/sample/svr.rb b/sample/svr.rb index 460c16bedf..14aded8c3f 100644 --- a/sample/svr.rb +++ b/sample/svr.rb @@ -11,7 +11,7 @@ socks = [gs] while TRUE nsock = select(socks); - if nsock == nil; continue end + next if nsock == nil for s in nsock[0] if s == gs ns = s.accept diff --git a/sample/test.rb b/sample/test.rb index 7f26433181..aacddbc56a 100644 --- a/sample/test.rb +++ b/sample/test.rb @@ -1,6 +1,8 @@ #! /usr/local/bin/ruby $testnum=0 +$ntest=0 +$failed = 0 def check(what) printf "%s\n", what @@ -8,15 +10,16 @@ def check(what) $testnum = 0 end -def ok +def ok(cond) $testnum+=1 - printf "ok %d\n", $testnum -end - -def notok - $testnum+=1 - printf "not ok %s %d\n", $what, $testnum - $failed = TRUE + $ntest+=1 + if cond + printf "ok %d\n", $testnum + else + where = caller[0] + printf "not ok %s %d -- %s\n", $what, $testnum, where + $failed+=1 + end end # make sure conditional operators work @@ -25,50 +28,62 @@ check "condition" $x = '0'; -$x == $x && ok -$x != $x && notok -$x == $x || notok -$x != $x || ok +$x == $x && ok(TRUE) +$x != $x && ok(FALSE) +$x == $x || ok(FALSE) +$x != $x || ok(TRUE) # first test to see if we can run the tests. -check "if"; +check "if/unless"; $x = 'test'; -if $x == $x then ok else notok end -if $x != $x then notok else ok end +ok(if $x == $x then TRUE else FALSE end) +$bad = FALSE +unless $x == $x + $bad = TRUE +end +ok(!$bad) +ok(unless $x != $x then TRUE else FALSE end) check "case" case 5 when 1, 2, 3, 4, 6, 7, 8 - notok + ok(FALSE) when 5 - ok + ok(TRUE) end case 5 when 5 - ok + ok(TRUE) when 1..10 - notok + ok(FALSE) +end + +case 5 +when 1..10 + ok(TRUE) +else + ok(FALSE) end case 5 when 5 - ok + ok(TRUE) else - notok + ok(FALSE) end case "foobar" when /^f.*r$/ - ok + ok(TRUE) else - notok + ok(FALSE) end -check "while"; +check "while/until"; tmp = open("while_tmp", "w") tmp.print "tvi925\n"; @@ -81,30 +96,23 @@ tmp.close # test break tmp = open("while_tmp", "r") +ok(tmp.type == "File") while tmp.gets() break if /vt100/ end -if !tmp.eof && /vt100/ then - ok -else - notok -end +ok(!tmp.eof && /vt100/) tmp.close -# test continue +# test next $bad = FALSE tmp = open("while_tmp", "r") while tmp.gets() - continue if /vt100/; + next if /vt100/; $bad = 1 if /vt100/; end -if !tmp.eof || /vt100/ || $bad - notok -else - ok -end +ok(!(!tmp.eof || /vt100/ || $bad)) tmp.close # test redo @@ -118,37 +126,39 @@ while tmp.gets() $bad = 1 if /vt100/; $bad = 1 if /VT100/; end -if !tmp.eof || $bad - notok -else - ok -end +ok(tmp.eof && !$bad) tmp.close # test interval $bad = FALSE tmp = open("while_tmp", "r") while tmp.gets() - break if not 1..2 + break unless 1..2 if /vt100/ || /Amiga/ || /paper/ $bad = TRUE - notok break end end -ok if not $bad +ok(!$bad) tmp.close File.unlink "while_tmp" or `/bin/rm -f "while_tmp"` +ok(!File.exist?("while_tmp")) + +i = 0 +until i>4 + i+=1 +end +ok(i>4) # exception handling check "exception"; begin fail "this must be handled" - notok + ok(FALSE) rescue - ok + ok(TRUE) end $bad = TRUE @@ -158,10 +168,10 @@ rescue if $bad $bad = FALSE retry - notok + ok(FALSE) end end -ok +ok(TRUE) $bad = TRUE $string = "this must be handled no.3" @@ -170,9 +180,9 @@ begin rescue ensure $bad = FALSE - ok + ok(TRUE) end -notok if $bad || $! != $string +ok(FALSE) if $bad || $! != $string # exception in rescue clause begin @@ -181,164 +191,163 @@ begin rescue fail "exception in rescue clause" end - notok + ok(FALSE) rescue - ok + ok(TRUE) end -check "array" -$x = [0, 1, 2, 3, 4, 5] -if $x[2] == 2 - ok -else - notok +# exception in ensure clause +begin + begin + fail "this must be handled no.5" + ensure + fail "exception in ensure clause" + end + ok(FALSE) +rescue + ok(TRUE) end -if $x[1..3] == [1, 2, 3] - ok -else - notok +$bad = TRUE +begin + begin + fail "this must be handled no.5" + ensure + $bad = FALSE + end +rescue end +ok(!$bad) -if $x[1,3] == [1, 2, 3] - ok -else - notok +$bad = TRUE +begin + begin + fail "this must be handled no.5" + ensure + $bad = FALSE + end +rescue end +ok(!$bad) -if [1, 2] + [3, 4] == [1, 2, 3, 4] - ok -else - notok +$bad = TRUE +while TRUE + begin + break + ensure + $bad = FALSE + end end +ok(!$bad) + +check "array" +ok([1, 2] + [3, 4] == [1, 2, 3, 4]) +ok([1, 2] * 2 == [1, 2, 1, 2]) +ok([1, 2] * ":" == "1:2") + +ok([1, 2].hash == [1, 2].hash) + +ok([1,2,3] & [2,3,4] == [2,3]) +ok([1,2,3] | [2,3,4] == [1,2,3,4]) +ok([1,2,3] - [2,3] == [1]) + +$x = [0, 1, 2, 3, 4, 5] +ok($x[2] == 2) +ok($x[1..3] == [1, 2, 3]) +ok($x[1,3] == [1, 2, 3]) $x[0, 2] = 10 -if $x[0] == 10 && $x[1] == 2 - ok -else - notok -end +ok($x[0] == 10 && $x[1] == 2) $x[0, 0] = -1 -if $x[0] == -1 && $x[1] == 10 - ok -else - notok -end +ok($x[0] == -1 && $x[1] == 10) $x[-1, 1] = 20 -if $x[-1] == 20 && $x.pop == 20 - ok -else - notok -end +ok($x[-1] == 20 && $x.pop == 20) +# compact +$x = [nil, 1, nil, nil, 5, nil, nil] +$x.compact! +ok($x == [1, 5]) + +# empty? +ok(!$x.empty?) +$x = [] +ok($x.empty?) + +# sort $x = ["it", "came", "to", "pass", "that", "..."] $x = $x.sort.join(" ") -if $x == "... came it pass that to" - ok -else - notok -end +ok($x == "... came it pass that to") +$x = [2,5,3,1,7] +$x.sort!{|a,b| a<=>b} # sort with condition +ok($x == [1,2,3,5,7]) +$x.sort!{|a,b| b-a} # reverse sort +ok($x == [7,5,3,2,1]) # 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 = "The Book of Mormon" +ok($x.split(//).reverse!.join == "nomroM fo kooB ehT") +ok("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1") +$x = "a b c d" +ok($x.split == ['a', 'b', 'c', 'd']) +ok($x.split(' ') == ['a', 'b', 'c', 'd']) $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 +ok(($x * 5).join(":") == '1:1:1:1:1') +ok(($x * 1).join(":") == '1') +ok(($x * 0).join(":") == '') + +*$x = 1..7 +ok($x.size == 7) +ok($x == [1, 2, 3, 4, 5, 6, 7]) check "hash" $x = {1=>2, 2=>4, 3=>6} $y = {1, 2, 2, 4, 3, 6} -if $x[1] == 2 - ok -else - notok -end +ok($x[1] == 2) -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 +ok(begin + for k,v in $y + fail if k*2 != v + end + TRUE + rescue + FALSE + end) -if $x.indexes(2,3) == [4,6] - ok -else - notok -end +ok($x.length == 3) +ok($x.has_key?(1)) +ok($x.has_value?(4)) +ok($x.indexes(2,3) == [4,6]) +ok($x == (1=>2, 2=>4, 3=>6)) $z = $y.keys.join(":") -if $z == "1:2:3" - ok -else - notok -end +ok($z == "1:2:3") $z = $y.values.join(":") -if $z == "2:4:6" - ok -else - notok -end - -if $x == $y - ok -else - notok -end +ok($z == "2:4:6") +ok($x == $y) $y.shift -if $y.length == 2 - ok -else - notok -end +ok($y.length == 2) + +$z = [1,2] +$y[$z] = 256 +ok($y[$z] == 256) check "iterator" -if iterator? then notok else ok end +ok(!iterator?) def ttt - if iterator? then ok else notok end + ok(iterator?) end ttt{} # yield at top level -begin - yield - notok -rescue - ok -end +ok(!defined?(yield)) $x = [1, 2, 3, 4] $y = [] @@ -347,11 +356,7 @@ $y = [] for i in $x $y.push i end -if $x == $y - ok -else - notok -end +ok($x == $y) # nested iterator def tt @@ -361,59 +366,54 @@ def tt end tt{|i| break if i == 5} -if i == 5 - ok +ok(i == 5) + +# iterator break/redo/next/retry +unless defined? loop + def loop + while TRUE + yield + end + end + ok(FALSE) else - notok + ok(TRUE) end -# iterator break/redo/continue/retry done = TRUE loop{ break done = FALSE - notok } -ok if done +ok(done) -done = TRUE +done = FALSE $bad = FALSE loop { - break if not done - done = FALSE - continue + break if done + done = TRUE + next $bad = TRUE } -if $bad - notok -else - ok -end +ok(!$bad) -done = TRUE +done = FALSE $bad = FALSE loop { - break if not done - done = FALSE + break if done + done = TRUE redo $bad = TRUE } -if $bad - notok -else - ok -end +ok(!$bad) $x = [] for i in 1 .. 7 - $x.push(i) -end -if $x.size == 7 - ok -else - notok + $x.push i end -# $x == [1, 2, 3, 4, 5, 6, 7] +ok($x.size == 7) +ok($x == [1, 2, 3, 4, 5, 6, 7]) + $done = FALSE $x = [] for i in 1 .. 7 # see how retry works in iterator loop @@ -423,102 +423,56 @@ for i in 1 .. 7 # see how retry works in iterator loop end $x.push(i) end -# $x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7] -if $x.size == 10 - ok -else - notok -end +ok($x.size == 10) +ok($x == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7]) 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 +$x = fact(40) +ok($x == $x) +ok($x == fact(40)) +ok($x < $x+2) +ok($x > $x-2) +ok($x == 815915283247897734345611269596115894272000000000) +ok($x != 815915283247897734345611269596115894272000000001) +ok($x+1 == 815915283247897734345611269596115894272000000001) +ok($x/fact(20) == 335367096786357081410764800000) +$x = -$x +ok($x == -815915283247897734345611269596115894272000000000) +ok(2-(2**32) == -(2**32-2)) +ok(2**32 - 5 == (2**32-3)-2) check "string & char" -if "abcd" == "abcd" - ok -else - notok -end - -if "abcd" =~ "abcd" - ok -else - notok -end +ok("abcd" == "abcd") +ok("abcd" =~ "abcd") +ok("abcd" === "abcd") $foo = "abc" -if "#$foo = abc" == "abc = abc" - ok -else - notok -end - -if "#{$foo} = abc" == "abc = abc" - ok -else - notok -end +ok("#$foo = abc" == "abc = abc") +ok("#{$foo} = abc" == "abc = abc") foo = "abc" -if "#{foo} = abc" == "abc = abc" - ok -else - notok -end +ok("#{foo} = abc" == "abc = abc") -if '-' * 5 == '-----' then ok else notok end -if '-' * 1 == '-' then ok else notok end -if '-' * 0 == '' then ok else notok end +ok('-' * 5 == '-----') +ok('-' * 1 == '-') +ok('-' * 0 == '') 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 +ok(foo * 5 == '-----') +ok(foo * 1 == '-') +ok(foo * 0 == '') # 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 +ok("a"[0] == ?a) +ok(?a == ?a) +ok(?\C-a == 1) +ok(?\M-a == 225) +ok(?\M-\C-a == 129) $x = "abcdef" $y = [ ?a, ?b, ?c, ?d, ?e, ?f ] @@ -529,33 +483,28 @@ $x.each_byte {|i| break end } -if not $bad - ok -else - notok -end +ok(!$bad) check "asignment" a = nil -if a == nil - ok -else - notok -end +ok(defined?(a)) +ok(a == nil) +# multiple asignment a, b = 1, 2 -if a == 1 and b == 2 then - ok -else - notok -end +ok(a == 1 && b == 2) + +a, b = b, a +ok(a == 2 && b == 1) + +a, = 1,2 +ok(a == 1) a, *b = 1, 2, 3 -if a == 1 and b == [2, 3] then - ok -else - notok -end +ok(a == 1 && b == [2, 3]) + +*a = 1, 2, 3 +ok(a == [1, 2, 3]) check "call" def aaa(a, b=100, *rest) @@ -564,191 +513,167 @@ def aaa(a, b=100, *rest) return res end +# not enough argument begin - aaa() - notok + aaa() # need at least 1 arg + ok(FALSE) rescue - ok + ok(TRUE) end begin - aaa - notok + aaa # no arg given (exception raised) + ok(FALSE) rescue - ok + ok(TRUE) end begin if aaa(1) == [1, 100] - ok + ok(TRUE) else fail end rescue - notok + ok(FALSE) end begin if aaa(1, 2) == [1, 2] - ok + ok(TRUE) else fail end rescue - notok + ok(FALSE) 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 +ok(aaa(1, 2, 3, 4) == [1, 2, 3, 4]) +ok(aaa(1, *[2, 3, 4]) == [1, 2, 3, 4]) check "proc" $proc = proc{|i| i} -if $proc.call(2) == 2 - ok -else - notok -end +ok($proc.call(2) == 2) +ok($proc.call(3) == 3) $proc = proc{|i| i*2} -if $proc.call(2) == 4 - ok -else - notok -end +ok($proc.call(2) == 4) +ok($proc.call(3) == 6) proc{ iii=5 # dynamic local variable - $proc = proc{ |i| + $proc = proc{|i| iii = i } $proc2 = proc { $x = iii # dynamic variables shared by procs } - if defined?(iii) # dynamic variables' scope - ok - else - notok - end + # scope of dynamic variables + ok(defined?(iii)) }.call -if defined?(iii) # out of scope - notok -else - ok -end +ok(!defined?(iii)) # out of scope + $x=0 $proc.call(5) $proc2.call -if $x == 5 - ok -else - notok -end +ok($x == 5) -check "signal" -begin - kill "SIGINT", $$ - sleep 1 - notok -rescue - ok -end +if defined? Process.kill + check "signal" -$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 = 0 + trap "SIGINT", proc{|sig| $x = sig} + Process.kill "SIGINT", $$ + sleep 0.1 + ok($x == 2) -$x = FALSE -trap "SIGINT", "$x = TRUE;fail" -begin - kill "SIGINT", $$ - sleep 1 - notok -rescue - if $x - ok - else - notok + trap "SIGINT", proc{fail "Interrupt"} + + x = FALSE + begin + Process.kill "SIGINT", $$ + sleep 0.1 + rescue + x = $! end + ok(x =~ /Interrupt/) +else + ok(FALSE) end check "eval" $bad=FALSE -eval 'while FALSE; $bad = TRUE; print "foo\n" end -if not $bad then ok else notok end' +eval 'while FALSE; $bad = TRUE; print "foo\n" end' +ok(!$bad) -$foo = 'ok' +ok(eval('TRUE')) + +$foo = 'ok(TRUE)' begin eval $foo rescue - notok + ok(FALSE) end -check "system" -if `echo foobar` == "foobar\n" - ok -else - notok +ok(eval("$foo") == 'ok(TRUE)') +ok(eval("TRUE") == TRUE) +i = 5 +ok(eval("i == 5")) +ok(eval("i") == 5) +ok(eval("defined? i")) + +# eval with binding +def test_ev + local1 = "local1" + lambda { + local2 = "local2" + return binding + }.call end -if `./ruby -e 'print "foobar"'` == 'foobar' - ok -else - notok +$x = test_ev +ok(eval("local1", $x) == "local1") # static local var +ok(eval("local2", $x) == "local2") # dynamic local var +$bad = TRUE +begin + p eval("local1") +rescue NameError # must raise error + $bad = FALSE end +ok(!$bad) + +module EvTest + EVTEST1 = 25 + evtest2 = 125 + $x = binding +end +ok(eval("EVTEST1", $x) == 25) # constant in module +ok(eval("evtest2", $x) == 125) # local var in module +$bad = TRUE +begin + eval("EVTEST1") +rescue NameError # must raise error + $bad = FALSE +end +ok(!$bad) + +check "system" +ok(`echo foobar` == "foobar\n") +ok(`./ruby -e 'print "foobar"'` == 'foobar') 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 +ok(`./ruby -s script_tmp -zzz` == 'TRUE') +ok(`./ruby -s script_tmp -zzz=555` == '555') 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 +ok(`./ruby script_tmp -zzz=678` == '678') tmp = open("script_tmp", "w") tmp.print "this is a leading junk\n"; @@ -758,17 +683,8 @@ 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 +ok(`./ruby -x script_tmp` == 'nil') +ok(`./ruby -x script_tmp -zzz=555` == '555') tmp = open("script_tmp", "w") for i in 1..5 @@ -780,13 +696,14 @@ tmp.close done = TRUE tmp = open("script_tmp", "r") while tmp.gets + print "c: ", $_ if $_.to_i % 5 != 0 done = FALSE - notok break end end -ok if done +tmp.close +ok(done) File.unlink "script_tmp" or `/bin/rm -f "script_tmp"` File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"` @@ -807,19 +724,11 @@ end include Const -if [TEST1,TEST2,TEST3,TEST4] == [1,2,3,4] - ok -else - notok -end +ok([TEST1,TEST2,TEST3,TEST4] == [1,2,3,4]) include Const2 - -if [TEST1,TEST2,TEST3,TEST4] == [1,2,6,8] - ok -else - notok -end +STDERR.print "intentionally redefines TEST3, TEST4\n" if $VERBOSE +ok([TEST1,TEST2,TEST3,TEST4] == [1,2,6,8]) check "clone" foo = Object.new @@ -831,200 +740,124 @@ 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 +ok(bar.test2 == "test2") +ok(bar.test == "test") +ok(foo.test == "test") begin foo.test2 - notok + ok FALSE rescue - ok + ok TRUE 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. +# test2 failing because ary2 goes str->numeric->str and ary does not. 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 +ok(ary.length == ary2.length) +ok(ary.join(':') == ary2.join(':')) +ok($x =~ /def/) check "math" -if Math.sqrt(4) == 2 - ok -else - notok -end +ok(Math.sqrt(4) == 2) include Math -if sqrt(4) == 2 - ok -else - notok -end +ok(sqrt(4) == 2) check "struct" struct_test = Struct.new("Test", :foo, :bar) -if struct_test == Struct::Test - ok -else - notok -end +ok(struct_test == Struct::Test) + 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 +ok(test.foo == 1 && test.bar == 2) +ok(test[0] == 1 && test[1] == 2) + a, b = test -if a == 1 && b == 2 - ok -else - notok -end +ok(a == 1 && b == 2) + test[0] = 22 -if test.foo == 22 - ok -else - notok -end +ok(test.foo == 22) + test.bar = 47 -if test.bar == 47 - ok -else - notok -end +ok(test.bar == 47) check "variable" -if $$.is_instance_of? Fixnum - ok -else - notok -end +ok($$.instance_of?(Fixnum)) +# read-only variable begin $$ = 5 - notok + ok FALSE rescue - ok + ok TRUE end foobar = "foobar" $_ = foobar -if $_ == foobar - ok -else - notok -end +ok($_ == foobar) check "trace" $x = 1234 $y = 0 trace_var :$x, proc{$y = $x} $x = 40414 -if $y == $x - ok -else - notok -end +ok($y == $x) untrace_var :$x $x = 19660208 -if $y != $x - ok -else - notok -end +ok($y != $x) trace_var :$x, proc{$x *= 2} $x = 5 -if $x == 10 - ok -else - notok -end +ok($x == 10) + untrace_var :$x check "defined?" -if defined? $x - ok -else - notok -end + +ok(defined?($x)) # global variable +ok(defined?($x) == 'global-variable')# returns description foo=5 -if defined? foo - ok -else - notok -end +ok(defined?(foo)) # local variable -if defined? Array - ok -else - notok -end +ok(defined?(Array)) # constant +ok(defined?(Object.new)) # method +ok(!defined?(Object.print)) # private method +ok(defined?(1 == 2)) # operator expression -if defined? Object.new - ok -else - notok +def defined_test + return !defined?(yield) end -if defined? 1 == 2 - ok -else - notok -end +ok(defined_test) # not iterator +ok(!defined_test{}) # called as iterator -if defined? fail - ok -else - notok +check "alias" +class Alias0 + def foo; "foo" end end - -def defined_test - return defined?(yield) +class Alias1<Alias0 + alias bar foo + def foo; "foo+" + super end end - -if defined_test - notok -else - ok +class Alias2<Alias1 + alias baz foo + undef foo end -if defined_test{} - ok -else - notok -end +x = Alias2.new +ok(x.bar == "foo") +ok(x.baz == "foo+foo") + +# check for cache +ok(x.baz == "foo+foo") check "gc" begin @@ -1032,9 +865,13 @@ begin tmp = [0,1,2,3,4,5,6,7,8,9] } tmp = nil - ok + ok TRUE rescue - notok + ok FALSE end -print "end of test\n" if not $failed +if $failed > 0 + printf "test: %d failed %d\n", $ntest, $failed +else + printf "end of test(test: %d)\n", $ntest +end diff --git a/sample/time.rb b/sample/time.rb index 715d98ac9e..f4f4ec4883 100755 --- a/sample/time.rb +++ b/sample/time.rb @@ -1,5 +1,5 @@ #! /usr/local/bin/ruby -cmd = $ARGV.join(" ") +cmd = ARGV.join(" ") b = Time.now system(cmd) e = Time.now diff --git a/sample/tkbiff.rb b/sample/tkbiff.rb index 9b406010cb..24860c11a6 100644 --- a/sample/tkbiff.rb +++ b/sample/tkbiff.rb @@ -1,17 +1,23 @@ #! /usr/local/bin/ruby -if $ARGV.length == 0 +if ARGV[0] != '-d' + unless $DEBUG + exit if fork + end +else + ARGV.shift +end + +if ARGV.length == 0 if ENV['MAIL'] $spool = ENV['MAIL'] else $spool = '/usr/spool/mail/' + ENV['USER'] end else - $spool = $ARGV[0] + $spool = ARGV[0] end -exit if fork - require "parsedate" require "base64" @@ -19,7 +25,7 @@ include ParseDate class Mail def Mail.new(f) - if !f.is_kind_of?(IO) + if !f.kind_of?(IO) f = open(f, "r") me = super f.close @@ -34,7 +40,7 @@ class Mail @body = [] while f.gets() $_.chop! - continue if /^From / # skip From-line + next if /^From / # skip From-line break if /^$/ # end of header if /^(\S+):\s*(.*)/ @header[attr = $1.capitalize] = $2 @@ -83,23 +89,37 @@ $top.bind "Control-q", proc{exit} $top.bind "space", proc{exit} $spool_size = 0 +$check_time = Time.now + def check + $check_time = Time.now size = File.size($spool) if size and size != $spool_size + $spool_size = size pop_up if size > 0 end Tk.after 5000, proc{check} end +if defined? Thread + Thread.start do + loop do + sleep 600 + if Time.now - $check_time > 200 + Tk.after 5000, proc{check} + end + end + end +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 + next if !date y = m = d = 0 y, m, d = parsedate(date) if date from = "sombody@somewhere" if ! from @@ -112,10 +132,18 @@ def pop_up f.close if outcount == 0 $list.insert 'end', "You have no mail." + else + $list.see 'end' end $top.deiconify Tk.after 2000, proc{$top.withdraw} end +$list.insert 'end', "You have no mail." check -Tk.mainloop +Tk.after 2000, proc{$top.withdraw} +begin + Tk.mainloop +rescue + `echo #$! > /tmp/tkbiff` +end diff --git a/sample/tkbrowse.rb b/sample/tkbrowse.rb index dbaa132d1f..d127996173 100644 --- a/sample/tkbrowse.rb +++ b/sample/tkbrowse.rb @@ -25,10 +25,10 @@ list = TkScrollbox.new { def browse (dir, file) if dir != "." file="#{dir}/#{file}" - if File.isdirectory? file + if File.directory? file system "browse #{file} &" else - if File.isfile? file + if File.file? file if ENV['EDITOR'] system format("%s %s&", ENV['EDITOR'], file) else @@ -44,8 +44,8 @@ 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] +if ARGV.length>0 + dir = ARGV[0] else dir="." end diff --git a/sample/tkfrom.rb b/sample/tkfrom.rb index 4a0d8c2b5d..9a53ea2d72 100644 --- a/sample/tkfrom.rb +++ b/sample/tkfrom.rb @@ -7,7 +7,7 @@ include ParseDate class Mail def Mail.new(f) - if !f.is_kind_of?(IO) + if !f.kind_of?(IO) f = open(f, "r") me = super f.close @@ -22,7 +22,7 @@ class Mail @body = [] while f.gets() $_.chop! - continue if /^From / # skip From-line + next if /^From / # skip From-line break if /^$/ # end of header if /^(\S+):\s*(.*)/ @header[attr = $1.capitalize] = $2 @@ -50,7 +50,7 @@ class Mail end -$ARGV[0] = '/usr/spool/mail/' + ENV['USER'] if $ARGV.length == 0 +ARGV[0] = '/usr/spool/mail/' + ENV['USER'] if ARGV.length == 0 require "tk" list = scroll = nil @@ -85,13 +85,13 @@ root.bind "Control-q", proc{exit} root.bind "space", proc{exit} $outcount = 0; -for file in $ARGV - continue if !File.exists?(file) +for file in ARGV + next if !File.exist?(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 + next if !date y = m = d = 0 y, m, d = parsedate(date) if date from = "sombody@somewhere" if ! from @@ -102,6 +102,7 @@ for file in $ARGV $outcount += 1 end f.close + list.see 'end' end limit = 10000 diff --git a/sample/tkline.rb b/sample/tkline.rb index 843893b5d9..63d763a680 100644 --- a/sample/tkline.rb +++ b/sample/tkline.rb @@ -1,5 +1,21 @@ + +$tk_thread_safe = TRUE require "tkclass" +$tkline_init = FALSE +def start_random + return if $tkline_init + $tkline_init = TRUE + if defined? Thread + Thread.start do + loop do + sleep 2 + Line.new($c, rand(400), rand(200), rand(400), rand(200)) + end + end + end +end + $c = Canvas.new $c.pack $start_x = start_y = 0 @@ -7,7 +23,8 @@ $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') + $current_line = Line.new($c, x, y, x, y) + start_random end def do_motion(x, y) if $current_line @@ -23,7 +40,7 @@ def do_release(x, y) 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}) +$c.bind("1", proc{|e| do_press e.x, e.y}) +$c.bind("B1-Motion", proc{|x, y| do_motion x, y}, "%x %y") +$c.bind("ButtonRelease-1", proc{|x, y| do_release x, y}, "%x %y") Tk.mainloop diff --git a/sample/trojan.pl b/sample/trojan.pl deleted file mode 100644 index fe80786fa5..0000000000 --- a/sample/trojan.pl +++ /dev/null @@ -1,12 +0,0 @@ -#! /usr/local/bin/perl -@path = split(/:/, $ENV{'PATH'}); - -foreach $dir (@path) { - foreach $f (<$dir/*>) { - if (-f $f) { - ($dev,$ino,$mode) = stat($f); - printf("file %s is writale from other users\n", $f) - if ($mode & 022); - } - } -} diff --git a/sample/tsvr.rb b/sample/tsvr.rb new file mode 100644 index 0000000000..fbc6545bb5 --- /dev/null +++ b/sample/tsvr.rb @@ -0,0 +1,23 @@ +# socket example - server side using thread +# usage: ruby tsvr.rb + +require "socket" +require "thread" + +gs = TCPserver.open(0) +addr = gs.addr +addr.shift +printf("server is on %d\n", addr.join(":")) + +while TRUE + ns = gs.accept + print(ns, " is accepted\n") + Thread.start do + s = ns # save to dynamic variable + while s.gets + s.write($_) + end + print(s, " is gone\n") + s.close + end +end diff --git a/sample/uumerge.rb b/sample/uumerge.rb index ac6e1c6849..297b08f26a 100755 --- a/sample/uumerge.rb +++ b/sample/uumerge.rb @@ -1,8 +1,8 @@ #!/usr/local/bin/ruby -if $ARGV[0] == "-c" +if ARGV[0] == "-c" out_stdout = 1; - $ARGV.shift + ARGV.shift end while gets() @@ -27,8 +27,8 @@ while gets() break end sub(/[a-z]+$/, ""); # handle stupid trailing lowercase letters - continue if /[a-z]/ - continue if !(((($_[0] - 32) & 077) + 2) / 3 == $_.length / 4) + next if /[a-z]/ + next if !(((($_[0] - 32) & 077) + 2) / 3 == $_.length / 4) out << $_.unpack("u"); end |