diff options
| author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-12-19 20:56:37 +0000 |
|---|---|---|
| committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-12-19 20:56:37 +0000 |
| commit | 6f07df121006daeb3347426a35df432d6927f6bb (patch) | |
| tree | f322774139b892603c027ac385f2d6e73852ca77 /sample | |
| parent | 2c917203b7a4a53cb9685ba387ab9231261e5098 (diff) | |
| download | ruby-6f07df121006daeb3347426a35df432d6927f6bb.tar.gz ruby-6f07df121006daeb3347426a35df432d6927f6bb.tar.xz ruby-6f07df121006daeb3347426a35df432d6927f6bb.zip | |
Improve.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@3186 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample')
| -rw-r--r-- | sample/occur2.rb | 4 | ||||
| -rw-r--r-- | sample/pi.rb | 2 | ||||
| -rw-r--r-- | sample/rcs.rb | 20 | ||||
| -rw-r--r-- | sample/time.rb | 2 | ||||
| -rw-r--r-- | sample/trojan.rb | 2 | ||||
| -rw-r--r-- | sample/tsvr.rb | 6 |
6 files changed, 13 insertions, 23 deletions
diff --git a/sample/occur2.rb b/sample/occur2.rb index b8ecf7090..53885c0ba 100644 --- a/sample/occur2.rb +++ b/sample/occur2.rb @@ -2,9 +2,9 @@ # usege: ruby occur2.rb file.. freq = {} while gets() - for word in $_.split(/\W+/) + for word in split(/\W+/) begin - freq[word] = freq[word] + 1 + freq[word] += 1 rescue NameError freq[word] = 1 end diff --git a/sample/pi.rb b/sample/pi.rb index 49067cc34..63be97428 100644 --- a/sample/pi.rb +++ b/sample/pi.rb @@ -2,7 +2,7 @@ k, a, b, a1, b1 = 2, 4, 1, 12, 4 -while TRUE +loop do # 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 diff --git a/sample/rcs.rb b/sample/rcs.rb index 7c091936f..0bdf81c45 100644 --- a/sample/rcs.rb +++ b/sample/rcs.rb @@ -8,22 +8,23 @@ w = 20.0 # distance between eyes h =1.0 # distance from screen and base plane d = 0.2 # z value unit ss="abcdefghijklmnopqrstuvwxyz0123456789#!$%^&*()-=\\[];'`,./" -rnd = srand() +rnd = srand() # You don't actually need this in ruby - srand() is called + # on the first call of rand(). while gets() # print($_) xr = -hdw; y = h * 1.0; maxxl = -999 - s = ""; + s = "" while xr < hdw x = xr * (1 + y) - y * w / 2 i = (x / (1 + h) + sw / 2) - if (1 < i && i < $_.length); + if (1 < i && i < $_.length) c = $_[i, 1].to_i else c = 0 end y = h - d * c - xl = xr - w * y / (1 + y); + xl = xr - w * y / (1 + y) if xl < -hdw || xl >= hdw || xl <= maxxl tt = rand(ss.length) c = ss[tt, 1] @@ -36,14 +37,3 @@ while gets() end print(s, "\n") end - - - - - - - - - - - diff --git a/sample/time.rb b/sample/time.rb index 1624fdf99..84fa9e1a8 100644 --- a/sample/time.rb +++ b/sample/time.rb @@ -5,4 +5,4 @@ system(cmd) e = Time.now ut, st, cut, cst = Time.times.to_a total = (e - b).to_f -printf STDERR, "%11.1f real %11.1f user %11.1f sys\n", total, cut, cst +STDERR.printf "%11.1f real %11.1f user %11.1f sys\n", total, cut, cst diff --git a/sample/trojan.rb b/sample/trojan.rb index 6e9b18f50..3a35ac21c 100644 --- a/sample/trojan.rb +++ b/sample/trojan.rb @@ -5,7 +5,7 @@ path = ENV['PATH'].split(File::PATH_SEPARATOR) for dir in path if File.directory?(dir) for f in d = Dir.open(dir) - fpath = dir+"/"+f + fpath = File.join(dir, f) if File.file?(fpath) && (File.stat(fpath).mode & 022) != 0 printf("file %s is writable from other users\n", fpath) end diff --git a/sample/tsvr.rb b/sample/tsvr.rb index 8b019bdb7..52cdd32dc 100644 --- a/sample/tsvr.rb +++ b/sample/tsvr.rb @@ -8,11 +8,11 @@ addr = gs.addr addr.shift printf("server is on %s\n", addr.join(":")) -while TRUE +loop do Thread.start(gs.accept) do |s| print(s, " is accepted\n") - while s.gets - s.write($_) + while line = s.gets + s.write(line) end print(s, " is gone\n") s.close |
