Forum: Ruby Destroy object from memory.. Object.const_destroy?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aaron S. (Guest)
on 2007-07-04 22:55
Hey all,

If I'm instantiating a instance of a class at using Object.const_get.
How can I destroy it so that next time it reloads and code changes will
take effect?

Thanks.
Yukihiro M. (Guest)
on 2007-07-05 18:34
(Received via mailing list)
Hi,

In message "Re: Destroy object from memory.. Object.const_destroy?"
    on Thu, 5 Jul 2007 03:56:01 +0900, Aaron S.
<removed_email_address@domain.invalid> writes:

|If I'm instantiating a instance of a class at using Object.const_get.
|How can I destroy it so that next time it reloads and code changes will
|take effect?

I am not sure what you mean here.  You don't instantiate anything
using const_get.  You just get the reference to the existing object.
If you want to redefine class objects (without warning), you remove
the constant using remove_const(), and reload the program using
load().  But I'd recommend restarting the program, unless reloading is
absolutely necessary.

              matz.
Aaron S. (Guest)
on 2007-07-05 18:40
Yukihiro M. wrote:
> Hi,
>
> In message "Re: Destroy object from memory.. Object.const_destroy?"
>     on Thu, 5 Jul 2007 03:56:01 +0900, Aaron S.
> <removed_email_address@domain.invalid> writes:
>
> |If I'm instantiating a instance of a class at using Object.const_get.
> |How can I destroy it so that next time it reloads and code changes will
> |take effect?
>
> I am not sure what you mean here.  You don't instantiate anything
> using const_get.  You just get the reference to the existing object.
> If you want to redefine class objects (without warning), you remove
> the constant using remove_const(), and reload the program using
> load().  But I'd recommend restarting the program, unless reloading is
> absolutely necessary.
>
>               matz.

Thanks,

That's pretty much what I was looking for. The problem I'm having is
with a plugin in Rails. Once it runs, if code is executed, then changed
at all, I have to restart Rails. Default rails controllers update just
fine. They're probably using the remove_const(). But with my plugin I
want to have the same functionality...

Are there any major risks doing this? Wouldn't seem like it.
Thanks
James G. (Guest)
on 2007-07-05 18:52
(Received via mailing list)
On Jul 5, 2007, at 9:40 AM, Aaron S. wrote:

>> changes will
>>               matz.
>
> Thanks,
>
> That's pretty much what I was looking for. The problem I'm having is
> with a plugin in Rails. Once it runs, if code is executed, then
> changed
> at all, I have to restart Rails. Default rails controllers update just
> fine. They're probably using the remove_const(). But with my plugin I
> want to have the same functionality...

Are you talking about the way Rails reloads classes in development
mode?  You can enable this in your own classes using:

   class MyClass
     include Reloadable
   end

James Edward G. II
Aaron S. (Guest)
on 2007-07-05 19:02
>> That's pretty much what I was looking for. The problem I'm having is
>> with a plugin in Rails. Once it runs, if code is executed, then
>> changed
>> at all, I have to restart Rails. Default rails controllers update just
>> fine. They're probably using the remove_const(). But with my plugin I
>> want to have the same functionality...
>
> Are you talking about the way Rails reloads classes in development
> mode?  You can enable this in your own classes using:
>
>    class MyClass
>      include Reloadable
>    end
>
> James Edward G. II

That's another good solution. Thanks.
This topic is locked and can not be replied to.