From 35247a52ef719584a59ae9c518523f0ee825c8e3 Mon Sep 17 00:00:00 2001 From: matz Date: Wed, 24 Mar 1999 08:52:35 +0000 Subject: 990324 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_3@409 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- sample/cal.rb | 10 +++++----- sample/from.rb | 2 ++ sample/mine.rb | 2 +- sample/sieve.rb | 21 +++++++++------------ 4 files changed, 17 insertions(+), 18 deletions(-) (limited to 'sample') diff --git a/sample/cal.rb b/sample/cal.rb index 50865dbb8f..c65f7a70f8 100644 --- a/sample/cal.rb +++ b/sample/cal.rb @@ -1,7 +1,7 @@ #! /usr/local/bin/ruby # cal.rb (bsd compatible version): Written by Tadayoshi Funaba 1998, 1999 -# $Id: bsdcal.rb,v 1.3 1999/02/06 08:52:21 tadf Exp $ +# $Id: bsdcal.rb,v 1.4 1999/03/06 02:05:59 tadf Exp $ require 'date2' @@ -70,10 +70,10 @@ end while /^-([^-].*)$/no =~ $*[0] a = $1 - if /^c(.+)?$/no =~ a then - if $1 then + if /^c(.+)?$/no =~ a + if $1 $cc = $1.downcase - elsif $*.length >= 2 then + elsif $*.length >= 2 $cc = $*[1].downcase $*.shift else @@ -109,7 +109,7 @@ end usage unless m.nil? or (1..12) === m usage unless y >= -4712 $w = if $jd then 3 else 2 end -unless $yr then +unless $yr print cal(m, y, gs) else print y.to_s.center(((($w + 1) * 7) - 1) * diff --git a/sample/from.rb b/sample/from.rb index 93b6c4bade..c458b4ea95 100644 --- a/sample/from.rb +++ b/sample/from.rb @@ -17,6 +17,8 @@ class String len += 1 me = self[0, len].ljust(len) if me =~ /.$/ and $&.size == 2 + p me[-2..-1] + me[-2..-1] = ' ' me[-2, 2] = ' ' end me.chop! diff --git a/sample/mine.rb b/sample/mine.rb index f18d6c6b03..fd326482a5 100644 --- a/sample/mine.rb +++ b/sample/mine.rb @@ -168,6 +168,6 @@ begin end end ensure - system("stty -raw echo") + system("stty -raw echo") end print "\n" diff --git a/sample/sieve.rb b/sample/sieve.rb index 5e9f792d81..359c185f20 100644 --- a/sample/sieve.rb +++ b/sample/sieve.rb @@ -1,17 +1,14 @@ # sieve of Eratosthenes -sieve = [] max = Integer(ARGV.shift || 100) +sieve = [] +for i in 2 .. max + sieve[i] = i +end -print "1" -for i in 2 .. max - begin - for d in sieve - fail if i % d == 0 - end - print ", " - print i - sieve.push(i) - rescue +for i in 2 .. Math.sqrt(max) + next unless sieve[i] + (i*i).step(max, i) do |j| + sieve[j] = nil end end -print "\n" +puts sieve.compact.join ", " -- cgit v1.2.3