Hi,
I also don't think you can reassign self without digging into Ruby's
source code. My idea was to at least *fake* an object transformation by
making the nil instance a wrapper for an array:
#------------------
class NilClass
def push *values
@array = values
values.methods.each do |method|
define_singleton_method method do |*args, &block|
@array.send method, *args, &block
end
end
end
end
a = nil
a.push 1, 2
p a
puts a.class
#------------------
But then I realized that nil is actually a singleton object (there's
only one nil). This somewhat makes the whole thing pointless, because
any change to a nil instance would affect all "nils".
I don't see the point in this feature, anyway. It perhaps would make
some sense if you didn't have to set the variable in the first place but
could magically create a new array by calling the push method on a new
identifier. PHP has this, for example:
$x[0] = "abc"; // this makes $x an array
But when you have to write "a = nil" first, then you might as well write
"a = []" and leave out all the magic.
--
Posted via http://www.ruby-forum.com/.