From: David Madison Date: 2012-05-22T13:43:58+09:00 Subject: Changing self class from inside a method?? Let's start off with the assumption I want a method that allows an object to transform itself into another object. In other words: begClass = a.class a.changeToSomethingElse(42) endClass = a.class # Where begClass != endClass As a more specific example, consider some hopeful code for allowing auto-creation of arrays from nil, perl-ish style: class Nil def push(*els) a = Array.new a.push(*els) #self = a # Nope! #self.class = Array # Nope! end end This would allow: a = nil a.push(1,2,3) puts "#{a.class}" I know we have things like to_a, but that requires an assigment instead of just the method call. Let's just assume we need a way to do it like this. Is this just considered impossible, or is there a way to change/convert the class of self from inside a method? I can imagine it would cause complications the moment the class changes, since we'd suddenly be in the method of the wrong class, but if any language had a way to do it, I figure it'd be ruby. -- Posted via http://www.ruby-forum.com/.