summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-02-24 04:31:29 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-02-24 04:31:29 +0000
commit3976feed73bf4ec27183824870ee077c2b5b00b1 (patch)
treecb461ab9246a2c8e13d373a8c2d2e6378e4db9cc /lib
parent51fa86ece2d2b0f72c86c70fe1314e412c7a9e68 (diff)
990224
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_3@405 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/e2mmap.rb269
-rw-r--r--lib/matrix.rb67
-rw-r--r--lib/tempfile.rb3
3 files changed, 206 insertions, 133 deletions
diff --git a/lib/e2mmap.rb b/lib/e2mmap.rb
index 3d72aaf..e04ed4a 100644
--- a/lib/e2mmap.rb
+++ b/lib/e2mmap.rb
@@ -1,17 +1,46 @@
#
# e2mmap.rb - for ruby 1.1
-# $Release Version: 1.2$
-# $Revision: 1.8 $
-# $Date: 1998/08/19 15:22:22 $
+# $Release Version: 2.0$
+# $Revision: 1.10 $
+# $Date: 1999/02/17 12:33:17 $
# by Keiju ISHITSUKA
#
# --
# Usage:
#
+# U1)
# class Foo
# extend Exception2MassageMapper
+# def_e2message ExistingExceptionClass, "message..."
# def_exception :NewExceptionClass, "message..."[, superclass]
+# ...
+# end
+#
+# U2)
+# module Error
+# extend Exception2MassageMapper
# def_e2meggage ExistingExceptionClass, "message..."
+# def_exception :NewExceptionClass, "message..."[, superclass]
+# ...
+# end
+# class Foo
+# include Exp
+# ...
+# end
+#
+# foo = Foo.new
+# foo.Fail ....
+#
+# U3)
+# module Error
+# extend Exception2MassageMapper
+# def_e2message ExistingExceptionClass, "message..."
+# def_exception :NewExceptionClass, "message..."[, superclass]
+# ...
+# end
+# class Foo
+# extend Exception2MessageMapper
+# include Error
# ...
# end
#
@@ -19,113 +48,149 @@
# Foo.Fail ExistingExceptionClass, arg...
#
#
-if VERSION < "1.1"
- require "e2mmap1_0.rb"
-else
+fail "Use Ruby 1.1" if VERSION < "1.1"
+
+module Exception2MessageMapper
+ @RCS_ID='-$Id: e2mmap.rb,v 1.10 1999/02/17 12:33:17 keiju Exp keiju $-'
+
+ E2MM = Exception2MessageMapper
+
+ def E2MM.extend_object(cl)
+ super
+ cl.bind(self) unless cl == E2MM
+ end
- module Exception2MessageMapper
- @RCS_ID='-$Id: e2mmap.rb,v 1.8 1998/08/19 15:22:22 keiju Exp keiju $-'
-
- E2MM = Exception2MessageMapper
+ # 以前との互換性のために残してある.
+ def E2MM.extend_to(b)
+ c = eval("self", b)
+ c.extend(self)
+ end
- def E2MM.extend_object(cl)
+ def bind(cl)
+ self.module_eval %[
+ def Raise(err = nil, *rest)
+ Exception2MessageMapper.Raise(self.type, err, *rest)
+ end
+ alias Fail Raise
+
+ def self.append_features(mod)
+ super
+ mod.extend Exception2MessageMapper
+ end
+ ]
+ end
+
+ # Fail(err, *rest)
+ # err: 例外
+ # rest: メッセージに渡すパラメータ
+ #
+ def Raise(err = nil, *rest)
+ E2MM.Raise(self, err, *rest)
+ end
+ alias Fail Raise
+
+ # 過去の互換性のため
+ alias fail! fail
+ def fail(err = nil, *rest)
+ begin
+ E2MM.Fail(self, err, *rest)
+ rescue E2MM::ErrNotRegisteredException
super
- cl.bind(self)
end
-
- # backward compatibility
- def E2MM.extend_to(b)
- c = eval("self", b)
- c.extend(self)
- end
-
- # public :fail
- alias fail! fail
+ end
+ class << self
+ public :fail
+ end
- #def fail(err = nil, *rest)
- # super
- #end
+
+ # def_e2message(c, m)
+ # c: exception
+ # m: message_form
+ # 例外cのメッセージをmとする.
+ #
+ def def_e2message(c, m)
+ E2MM.def_e2message(self, c, m)
+ end
+
+ # def_exception(c, m)
+ # n: exception_name
+ # m: message_form
+ # s: 例外スーパークラス(デフォルト: StandardError)
+ # 例外名``c''をもつ例外を定義し, そのメッセージをmとする.
+ #
+ def def_exception(n, m, s = StandardError)
+ E2MM.def_exception(self, n, m, s)
+ end
- def Fail(err = nil, *rest)
- Exception2MessageMapper.Fail Exception2MessageMapper::ErrNotRegisteredException, err.inspect
- end
-
- def bind(cl)
- self.module_eval %q^
- E2MM_ErrorMSG = {} unless self.const_defined?(:E2MM_ErrorMSG)
- # fail(err, *rest)
- # err: Exception
- # rest: Parameter accompanied with the exception
- #
- def self.Fail(err = nil, *rest)
- if form = E2MM_ErrorMSG[err]
- $! = err.new(sprintf(form, *rest))
- $@ = caller(0) if $@.nil?
- $@.shift
- # e2mm_fail()
- raise()
-# elsif self == Exception2MessageMapper
-# fail Exception2MessageMapper::ErrNotRegisteredException, err.to_s
- else
-# print "super\n"
- super
- end
- end
+ #
+ # Private definitions.
+ #
+ # {[class, exp] => message, ...}
+ @MessageMap = {}
+
+ # E2MM.def_exception(k, e, m)
+ # k: 例外を定義するクラス
+ # e: exception
+ # m: message_form
+ # 例外cのメッセージをmとする.
+ #
+ def E2MM.def_e2message(k, c, m)
+ E2MM.instance_eval{@MessageMap[[k, c]] = m}
+ c
+ end
+
+ # E2MM.def_exception(k, c, m)
+ # k: 例外を定義するクラス
+ # n: exception_name
+ # m: message_form
+ # s: 例外スーパークラス(デフォルト: StandardError)
+ # 例外名``c''をもつ例外を定義し, そのメッセージをmとする.
+ #
+ def E2MM.def_exception(k, n, m, s = StandardError)
+ n = n.id2name if n.kind_of?(Fixnum)
+ e = Class.new(s)
+ E2MM.instance_eval{@MessageMap[[k, e]] = m}
+ k.const_set(n, e)
+ end
- # backward compatibility
- def self.fail(err = nil, *rest)
- if form = E2MM_ErrorMSG[err]
- $! = err.new(sprintf(form, *rest))
- $@ = caller(0) if $@.nil?
- $@.shift
- # e2mm_fail()
- raise()
-# elsif self == Exception2MessageMapper
-# fail Exception2MessageMapper::ErrNotRegisteredException, err.to_s
- else
-# print "super\n"
- super
- end
- end
- class << self
- public :fail
- end
-
- # def_exception(c, m)
- # c: exception
- # m: message_form
- #
- def self.def_e2message(c, m)
- E2MM_ErrorMSG[c] = m
- end
-
- # def_exception(c, m)
- # n: exception_name
- # m: message_form
- # s: superclass_of_exception (default: Exception)
- # defines excaption named ``c'', whose message is ``m''.
- #
- #def def_exception(n, m)
- def self.def_exception(n, m, s = nil)
- n = n.id2name if n.kind_of?(Fixnum)
- unless s
- if defined?(StandardError)
- s = StandardError
- else
- s = Exception
- end
- end
- e = Class.new(s)
+ # Fail(klass, err, *rest)
+ # klass: 例外の定義されているクラス
+ # err: 例外
+ # rest: メッセージに渡すパラメータ
+ #
+ def E2MM.Raise(klass = E2MM, err = nil, *rest)
+ if form = e2mm_message(klass, err)
+ $! = err.new(sprintf(form, *rest))
+ $@ = caller(1) if $@.nil?
+ #p $@
+ #p __FILE__
+ $@.shift if $@[0] =~ /^#{Regexp.quote(__FILE__)}:/
+ raise
+ else
+ E2MM.Fail E2MM, ErrNotRegisteredException, err.inspect
+ end
+ end
+ class <<E2MM
+ alias Fail Raise
+ end
- const_set(n, e)
- E2MM_ErrorMSG[e] = m
- # const_get(:E2MM_ErrorMSG)[e] = m
- end
- ^
+ def E2MM.e2mm_message(klass, exp)
+ for c in klass.ancestors
+ if mes = @MessageMap[[c,exp]]
+ p mes
+ m = klass.instance_eval('"' + mes + '"')
+ return m
end
-
- extend E2MM
- def_exception(:ErrNotClassOrModule, "Not Class or Module")
- def_exception(:ErrNotRegisteredException, "not registerd exception(%s)")
end
+ nil
+ end
+ class <<self
+ alias message e2mm_message
+ end
+
+ E2MM.def_exception(E2MM,
+ :ErrNotRegisteredException,
+ "not registerd exception(%s)")
end
+
+
diff --git a/lib/matrix.rb b/lib/matrix.rb
index 64b0738..4767b4b 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -1,8 +1,9 @@
+#!/usr/local/bin/ruby
#
# matrix.rb -
# $Release Version: 1.0$
-# $Revision: 1.6 $
-# $Date: 1998/07/31 03:39:49 $
+# $Revision: 1.8 $
+# $Date: 1999/02/17 12:34:19 $
# Original Version from Smalltalk-80 version
# on July 23, 1985 at 8:37:17 am
# by Keiju ISHITSUKA
@@ -17,6 +18,8 @@
# :
# rown]
#
+# column: 列
+# row: 行
#
# module ExceptionForMatrix::
# Exceptions:
@@ -173,19 +176,19 @@
require "e2mmap.rb"
module ExceptionForMatrix
- Exception2MessageMapper.extend_to(binding)
-
+ extend Exception2MessageMapper
def_e2message(TypeError, "wrong argument type %s (expected %s)")
def_e2message(ArgumentError, "Wrong # of arguments(%d for %d)")
- def_exception("ErrDimensionMismatch", "\#{self.type} dimemsion mismatch")
+ def_exception("ErrDimensionMismatch", "\#{self.name} dimension mismatch")
def_exception("ErrNotRegular", "Not Regular Matrix")
def_exception("ErrOperationNotDefined", "This operation(%s) can\\'t defined")
end
class Matrix
- @RCS_ID='-$Id: matrix.rb,v 1.6 1998/07/31 03:39:49 keiju Exp keiju $-'
-
+ @RCS_ID='-$Id: matrix.rb,v 1.8 1999/02/17 12:34:19 keiju Exp keiju $-'
+
+# extend Exception2MessageMapper
include ExceptionForMatrix
# instance creations
@@ -337,7 +340,7 @@ class Matrix
from_col = param[2]
size_col = param[3]
else
- Matrix.fail ArgumentError, param.inspect
+ Matrix.Raise ArgumentError, param.inspect
end
rows = @rows[from_row, size_row].collect{
@@ -410,7 +413,7 @@ class Matrix
r = self * m
return r.column(0)
when Matrix
- Matrix.fail ErrDimensionMismatch if column_size != m.row_size
+ Matrix.Raise ErrDimensionMismatch if column_size != m.row_size
rows = (0 .. row_size - 1).collect {
|i|
@@ -434,7 +437,7 @@ class Matrix
def +(m)
case m
when Numeric
- Matrix.fail ErrOperationNotDefined, "+"
+ Matrix.Raise ErrOperationNotDefined, "+"
when Vector
m = Matrix.column_vector(m)
when Matrix
@@ -443,7 +446,7 @@ class Matrix
return x + y
end
- Matrix.fail ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
+ Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
rows = (0 .. row_size - 1).collect {
|i|
@@ -458,7 +461,7 @@ class Matrix
def -(m)
case m
when Numeric
- Matrix.fail ErrOperationNotDefined, "-"
+ Matrix.Raise ErrOperationNotDefined, "-"
when Vector
m = Matrix.column_vector(m)
when Matrix
@@ -467,7 +470,7 @@ class Matrix
return x - y
end
- Matrix.fail ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
+ Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
rows = (0 .. row_size - 1).collect {
|i|
@@ -499,7 +502,7 @@ class Matrix
end
def inverse
- Matrix.fail ErrDimensionMismatch unless square?
+ Matrix.Raise ErrDimensionMismatch unless square?
Matrix.I(row_size).inverse_from(self)
end
alias inv inverse
@@ -512,7 +515,7 @@ class Matrix
if (akk = a[k][k]) == 0
i = k
begin
- fail ErrNotRegular if (i += 1) > size
+ Matrix.Raise ErrNotRegular if (i += 1) > size
end while a[i][k] == 0
a[i], a[k] = a[k], a[i]
@rows[i], @rows[k] = @rows[k], @rows[i]
@@ -568,9 +571,9 @@ class Matrix
end
z
elsif other.kind_of?(Float) || defined?(Rational) && other.kind_of?(Rational)
- fail ErrOperationNotDefined, "**"
+ Matrix.Raise ErrOperationNotDefined, "**"
else
- fail ErrOperationNotDefined, "**"
+ Matrix.Raise ErrOperationNotDefined, "**"
end
end
@@ -663,6 +666,8 @@ class Matrix
case other
when Numeric
return Scalar.new(other), self
+ else
+ raise TypeError, "#{type} can't be coerced into #{other.type}"
end
end
@@ -725,7 +730,7 @@ class Matrix
when Numeric
Scalar.new(@value + other)
when Vector, Matrix
- Scalar.fail WrongArgType, other.type, "Numeric or Scalar"
+ Scalar.Raise WrongArgType, other.type, "Numeric or Scalar"
when Scalar
Scalar.new(@value + other.value)
else
@@ -739,7 +744,7 @@ class Matrix
when Numeric
Scalar.new(@value - other)
when Vector, Matrix
- Scalar.fail WrongArgType, other.type, "Numeric or Scalar"
+ Scalar.Raise WrongArgType, other.type, "Numeric or Scalar"
when Scalar
Scalar.new(@value - other.value)
else
@@ -765,7 +770,7 @@ class Matrix
when Numeric
Scalar.new(@value / other)
when Vector
- Scalar.fail WrongArgType, other.type, "Numeric or Scalar or Matrix"
+ Scalar.Raise WrongArgType, other.type, "Numeric or Scalar or Matrix"
when Matrix
self * _M.inverse
else
@@ -779,7 +784,7 @@ class Matrix
when Numeric
Scalar.new(@value ** other)
when Vector
- Scalar.fail WrongArgType, other.type, "Numeric or Scalar or Matrix"
+ Scalar.Raise WrongArgType, other.type, "Numeric or Scalar or Matrix"
when Matrix
other.powered_by(self)
else
@@ -802,7 +807,7 @@ class Vector
private_class_method :new
def Vector.[](*array)
- new(:init_elements, array, FALSE)
+ new(:init_elements, array, copy = FALSE)
end
def Vector.elements(array, copy = TRUE)
@@ -833,7 +838,7 @@ class Vector
# ENUMRATIONS
def each2(v)
- Vector.fail ErrDimensionMismatch if size != v.size
+ Vector.Raise ErrDimensionMismatch if size != v.size
0.upto(size - 1) do
|i|
yield @elements[i], v[i]
@@ -841,7 +846,7 @@ class Vector
end
def collect2(v)
- Vector.fail ErrDimensionMismatch if size != v.size
+ Vector.Raise ErrDimensionMismatch if size != v.size
(0 .. size - 1).collect do
|i|
yield @elements[i], v[i]
@@ -878,7 +883,7 @@ class Vector
when Matrix
self.covector * x
else
- s, x = X.corece(self)
+ s, x = X.coerce(self)
s * x
end
end
@@ -886,7 +891,7 @@ class Vector
def +(v)
case v
when Vector
- Vector.fail ErrDimensionMismatch if size != v.size
+ Vector.Raise ErrDimensionMismatch if size != v.size
els = collect2(v) {
|v1, v2|
v1 + v2
@@ -895,7 +900,7 @@ class Vector
when Matrix
Matrix.column_vector(self) + v
else
- s, x = v.corece(self)
+ s, x = v.coerce(self)
s + x
end
end
@@ -903,7 +908,7 @@ class Vector
def -(v)
case v
when Vector
- Vector.fail ErrDimensionMismatch if size != v.size
+ Vector.Raise ErrDimensionMismatch if size != v.size
els = collect2(v) {
|v1, v2|
v1 - v2
@@ -912,7 +917,7 @@ class Vector
when Matrix
Matrix.column_vector(self) - v
else
- s, x = v.corece(self)
+ s, x = v.coerce(self)
s - x
end
end
@@ -920,7 +925,7 @@ class Vector
# VECTOR FUNCTIONS
def inner_product(v)
- Vector.fail ErrDimensionMismatch if size != v.size
+ Vector.Raise ErrDimensionMismatch if size != v.size
p = 0
each2(v) {
@@ -980,6 +985,8 @@ class Vector
case other
when Numeric
return Scalar.new(other), self
+ else
+ raise TypeError, "#{type} can't be coerced into #{other.type}"
end
end
diff --git a/lib/tempfile.rb b/lib/tempfile.rb
index bf51ac2..ed376a5 100644
--- a/lib/tempfile.rb
+++ b/lib/tempfile.rb
@@ -28,13 +28,14 @@ class Tempfile < SimpleDelegator
}
end
- def initialize(basename, tmpdir = '/tmp')
+ def initialize(basename, tmpdir = nil)
umask = File.umask(0177)
tmpname = lock = nil
begin
n = 0
while true
begin
+ tmpdir ||= ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'] || '/tmp'
tmpname = sprintf('%s/%s.%d.%d', tmpdir, basename, $$, n)
lock = tmpname + '.lock'
unless File.exist?(lock)