Hi,
In message "[ruby-talk:00626] Next misbehavior (sorry :-)"
on 99/08/11, clemens.hintze / alcatel.de <clemens.hintze / alcatel.de> writes:
>Only `Object' and `Struct' instances seems to call `initialize'.
Matz said that initialize is a "getleman's agreement". Because, in
some cases, it is too late to call initialize after the construction
of object. For example, an array must be allocate memory when
construction. Many predefined classes have such property. (sorry, I
know my explain is not enough but I can't do well).
So, you can choose
(1) define MyString without ``< File''
require "delegate"
class MyString<DelegateClass(String)
def initialize(*args)
print "Enter `initialize' with args=`#{args}'\n"
super (args[0] || "")
print "After parent initialization\n"
end
def show
print to_s, "\n"
end
end
(2) redefine `MyString.new', not `MyString#nitialize.
class MyString<String
class << self
def new(*args)
res = super(args[0] || "")
res.instance_eval{ initialize(*args) }
res
end
end
def initialize(*args)
print "Enter `initialize' with args=`#{args}'\n"
print "After parent initialization\n"
end
def show
print to_s, "\n"
end
end
>Furthermore some classes have no `new' method. would it make sense to
>add one (for alibi purposes)?
It was requiested by me and some persons. Because we can't imagine a
`new' numeric.
-- gotoken