Is it possible to change the class of a Ruby object?

后端 未结 3 1135
-上瘾入骨i
-上瘾入骨i 2021-01-18 03:41

Is it possible to change the class of a Ruby object once it has been instantiated, something like:

class A
end

class B
end

a = A.new
a.class = B

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 03:56

    simple answer, no:

    NoMethodError: undefined method `class=' for #
    

    however you can delete methods and mix in modules and such so as to leave an object that looks totally different...

提交回复
热议问题