From: Charles Oliver Nutter Date: 2010-04-27T00:04:04+09:00 Subject: Re: DrX, an object inspector On Sun, Apr 25, 2010 at 3:23 PM, David Masover wrote: > On Sunday 25 April 2010 02:41:07 am Charles Oliver Nutter wrote: > I've raised this question before, and people seem to disagree with me, but: > >> # changing the object's class(!?) >> class Foo; end >> p obj.class # => 'Object' >> obj_ref.setMetaClass(JRuby.reference(Foo)) >> p obj.class # => 'Foo' > > I think it should be possible to do that from within Ruby. > > But I can't even get most people behind this suggestion: > > class A; end > class B; end > A.instance_method(:to_s).bind(B.new) > > That gives me a type error, which just seems bizarre, especially after the > relative freedom of JavaScript. Ah, well. The main issue that comes up with this is that classes with native methods (like all the core classes) would have to be given special treatment. You couldn't, for example, rebind a String method to Array or an Array subclass since the in-memory structure is different. In short, you'd at best end up with methods that don't function because they're not attached to an array, and at worst with methods that outright segfault. There's no technical reason you couldn't allow rebinding of Ruby methods though. That's roughly possible in JRuby if you go "under the covers": require 'jruby' class Foo def foo puts "This is foo in #{self.class}" end end class Bar end foo_ref = JRuby.reference(Foo) bar_ref = JRuby.reference(Bar) bar_ref.add_method('bar', foo_ref.search_method('foo')) Bar.new.bar # => "This is foo in Bar" ...of course, the devil's in the details. - Charlie