From: "David A. Black" Date: 2003-12-14T23:59:21+09:00 Subject: Re: inplace assignment On Sun, 14 Dec 2003, T. Onoma wrote: > On Sunday 14 December 2003 07:49 am, Hal Fulton wrote: > > OK, I thought that was what you meant. > > > > String also has a replace. But there's no general Object#replace. > > > > My impression is that this in impossible in general, and for > > immediate values such as Fixnums, "even more impossible." > > > > The idea of doing this with a constant is scary to me. It's bad > > enough that a constant String or Array can be changed. And it's > > even scarier to think of doing that with something like a > > Fixnum. > > > > What's the situation where you'd want to do this? > > Well, the reason its a constant is b/c its a class. It's funny how things come > up. I only recently learned that when you modify a class, all previously > defined objects of that class are effected. But what if you want to pull the > entire "rug out", so to speak, and replace a class with a "duck similiar" > class? s = String # now it's a variable As for modifying a class: the objects are affected in the sense that the methods in their lookup path have changed. They themselves are not changed at all. I'm not sure what you mean by "duck similar" (the notion of duck typing doesn't scale well to "duck "), but assuming you mean a class that defines methods with the same names and arities as another class, the best thing would probably be to do: module A; # bunch of methods; end module B; # same-named methods as A; end class C; include A; end class C; include B; end # when you want the new ones or class C; include A; end c = C.new obj.extend(B) > Okay, so it's a bit crazy. But I actually came across a use for this. Luckly > my situation is specialized --I am replacing the class with a subclass of it, > so I discovered that I could just do this: > > class MyClass < MyClass Yikes. I *think* that resorting to that is a reasonably good indication that you might want to look for a new program design :-) David -- David A. Black dblack@wobblini.net