On Sun, Feb 7, 2016 at 11:55 AM, Robert Klemme
<shortcutter / googlemail.com> wrote:
> On Sat, Feb 6, 2016 at 10:48 AM, A Berger <aberger7890 / gmail.com> wrote:
>> Hello
>> Does anyone know a command to reset an irb-session to a clean state (like
>> ending and restarting irb)? - on the mobile restarting it is not optimal.
>
> What does "not optimal" mean?
>
>> If not thats the purpose of GC.
>
> The purpose of GC is to free memory of no longer referenced objects,
> nothing else. I have no idea what you want to convey with that
> sentence of yours.
>
>> Can I free the master-object (main?) or do I have to get a tree of all
>> objects and clear them.
>> how can this be done with classes (=constants)? - Setting to nil is not the
>> same as unsetting.
>
> In terms of effects on GC of the formerly referenced object it is,
> because you get rid of one reference.
>
>> class A
>> def x end
>> end
>> A=nil # still defined
>
>> Is it done with freeing all classes+global vars?
>
> I wouldn't worry about this: irb is a tool mostly used for interactive
> sessions to test things. If you want a clean slate, exit and reenter.

I has an idea! Here's another solution that might work better in case
of exiting irb and reentering is too costly:

def session()
  fork.tap {|ch| ch and (Process.wait(ch);puts "#{ch} finished")}
end

Put that in your ~/.irbrc if  possible or execute directly via

fork.tap {|ch| ch and (Process.wait(ch);puts "#{ch} finished")}

or shorter

fork.tap {|c| c and Process.wait(c)}

That will basically let the initial session intact (i.e. keeping all
the startup initialization) and work in a child process:

irb(main):010:0> def session() fork.tap {|ch| ch and
(Process.wait(ch);puts "#{ch} finished")} end
=> :session
irb(main):011:0> $$
=> 3566
irb(main):012:0> a
NameError: undefined local variable or method `a' for main:Object
from (irb):12
from /usr/bin/irb:11:in `<main>'
irb(main):013:0> session
=> nil
irb(main):014:0> $$
=> 3591
irb(main):015:0> a
NameError: undefined local variable or method `a' for main:Object
from (irb):15
from /usr/bin/irb:11:in `<main>'
irb(main):016:0> a=99
=> 99
irb(main):017:0> a
=> 99
irb(main):018:0> exit
3591 finished
=> 3591
irb(main):014:0> a
NameError: undefined local variable or method `a' for main:Object
from (irb):14
from /usr/bin/irb:11:in `<main>'

You can add some whistles and bells, e.g. changing the prompt in the
child so you know where you are.

Kind regards

robert


-- 
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>