Reset the whole state

Discussion in 'Ruby' started by Remi Gillig, Jun 13, 2008.

  1. Remi Gillig

    Remi Gillig Guest

    Hello,

    I just seached about this problem and found two 2 years old threads :
    http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/875a099efd08f70d
    http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/3e6fa1a935e50697

    Each of these talk about how to reset the whole Ruby state, like
    starting from a clean interpreter but without rerunning it. More or
    less like emptying everything and putting back the predefined
    constants, modules and classes.

    The first thread has a solution which is using remove_const to not be
    able to create classes anymore but this actually does not work
    properly because you can still do this :
    # class Hello end
    # $hello = Hello
    # Object.instance_eval("remove_const :Hello")
    # h = $hello.new

    I'm quite surprised that there was nothing about this in over 2 years
    of development. Maybe there are some explanation inside the Ruby
    Hacking Guide but most of it is in Japanese.

    Anybody has any idea on how to implement this? Thanks.

    Remi Gillig.
     
    Remi Gillig, Jun 13, 2008
    #1
    1. Advertising

  2. On 13.06.2008 16:57, Remi Gillig wrote:
    > Hello,
    >
    > I just seached about this problem and found two 2 years old threads :
    > http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/875a099efd08f70d
    > http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/3e6fa1a935e50697
    >
    > Each of these talk about how to reset the whole Ruby state, like
    > starting from a clean interpreter but without rerunning it. More or
    > less like emptying everything and putting back the predefined
    > constants, modules and classes.
    >
    > The first thread has a solution which is using remove_const to not be
    > able to create classes anymore but this actually does not work
    > properly because you can still do this :
    > # class Hello end
    > # $hello = Hello
    > # Object.instance_eval("remove_const :Hello")
    > # h = $hello.new
    >
    > I'm quite surprised that there was nothing about this in over 2 years
    > of development. Maybe there are some explanation inside the Ruby
    > Hacking Guide but most of it is in Japanese.
    >
    > Anybody has any idea on how to implement this? Thanks.


    I am wondering what you need that for. I guess removal is pretty
    complex and thus time consuming because even a freshly started Ruby
    interpreter does contain some classes and method definitions. And
    creating a process isn't that expensive.

    Having said that, maybe you can look at how Ruby can be embedded in C
    programs. Then you could create a new initial context if something like
    that exists.

    Cheers

    robert
     
    Robert Klemme, Jun 13, 2008
    #2
    1. Advertising

  3. Remi Gillig

    Remi Gillig Guest

    On Jun 13, 6:09 pm, Robert Klemme <> wrote:
    > On 13.06.2008 16:57, Remi Gillig wrote:
    >
    >
    >
    > > Hello,

    >
    > > I just seached about this problem and found two 2 years old threads :
    > >http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/87...
    > >http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/3e...

    >
    > > Each of these talk about how to reset the whole Ruby state, like
    > > starting from a clean interpreter but without rerunning it. More or
    > > less like emptying everything and putting back the predefined
    > > constants, modules and classes.

    >
    > > The first thread has a solution which is using remove_const to not be
    > > able to create classes anymore but this actually does not work
    > > properly because you can still do this :
    > > # class Hello end
    > > # $hello = Hello
    > > # Object.instance_eval("remove_const :Hello")
    > > # h = $hello.new

    >
    > > I'm quite surprised that there was nothing about this in over 2 years
    > > of development. Maybe there are some explanation inside the Ruby
    > > Hacking Guide but most of it is in Japanese.

    >
    > > Anybody has any idea on how to implement this? Thanks.

    >
    > I am wondering what you need that for. I guess removal is pretty
    > complex and thus time consuming because even a freshly started Ruby
    > interpreter does contain some classes and method definitions. And
    > creating a process isn't that expensive.
    >
    > Having said that, maybe you can look at how Ruby can be embedded in C
    > programs. Then you could create a new initial context if something like
    > that exists.
    >
    > Cheers
    >
    > robert


    It's actually embedding that I'm using right now. I am using Ruby as a
    script language for a code editor to perform operations on text etc.
    There is a script which is loaded when the program loads which
    contains
    some classes defined by the user for example. I would like to be able
    to
    reload this script without having warnings about constants, having
    classes mixed together if the user changed the name of a function
    between the loadings or still having the same global variables.

    Remi Gillig.
     
    Remi Gillig, Jun 14, 2008
    #3
  4. Hi Remi,

    I have the same approach that you describe in your post regarding
    embedded Ruby interpreter. I did some trial based on dl library in C to
    dynamically open and close Ruby interpreter but I had some issue due to
    dlclose() behavior when a so library is loaded into Ruby.

    Do you find any solution to restart your Ruby interpreter?

    Thanks,
    David

    Remi Gillig wrote:
    > On Jun 13, 6:09 pm, Robert Klemme <> wrote:
    >> > Each of these talk about how to reset the whole Ruby state, like
    >> > # h = $hello.new

    >> creating a process isn't that expensive.
    >>
    >> Having said that, maybe you can look at how Ruby can be embedded in C
    >> programs. Then you could create a new initial context if something like
    >> that exists.
    >>
    >> Cheers
    >>
    >> robert

    >
    > It's actually embedding that I'm using right now. I am using Ruby as a
    > script language for a code editor to perform operations on text etc.
    > There is a script which is loaded when the program loads which
    > contains
    > some classes defined by the user for example. I would like to be able
    > to
    > reload this script without having warnings about constants, having
    > classes mixed together if the user changed the name of a function
    > between the loadings or still having the same global variables.
    >
    > Remi Gillig.


    --
    Posted via http://www.ruby-forum.com/.
     
    David Marchaland, Feb 9, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Weng Tianxiang
    Replies:
    7
    Views:
    1,172
    Mike Treseler
    Nov 25, 2003
  2. Grant Rettke
    Replies:
    0
    Views:
    454
    Grant Rettke
    Mar 16, 2005
  3. Andy Fish
    Replies:
    1
    Views:
    2,510
    =?Utf-8?B?RG91ZyBTYWxvbW9u?=
    Jan 27, 2006
  4. \A_Michigan_User\
    Replies:
    2
    Views:
    938
    \A_Michigan_User\
    Aug 21, 2006
  5. Alan Isaac
    Replies:
    6
    Views:
    279
    Alan Isaac
    Apr 25, 2007
Loading...

Share This Page