Search code examples
jupyteriruby

Jupyter and IRuby: how to reset all variables befor executing a cell?


For study I am using IRuby in Jupyter. How to reset all all variables before executing a cell?

I need the equivalent of %reset for IPython.

I want to be sure that values from previous cells don't interfere with the ones I am working on.


Solution

  • Short answer

    Install Pry.

    gem install pry
    

    then use

    cd Module.new
    
    a = 1
    b = 2
    c = 3
    d = a + b + c + d
    puts d
    
    exit
    

    Answer

    IRuby does not have magic commands. So you can't use %reset.

    Unfortunately, IRuby defines local variables at the top level. Ruby is a language that does not have the right way to delete local variables once you defined it.

    The good news is that you can use Pry commands available in IRuby.

    Open Jupyter, and write the first line of the cell.

    cd Module.new
    

    cd is a Pry command that is used to move into a new object (or scope) inside a Pry session. So cd Module.new means you create a new disposable nameless module object, as a custom sandbox, and then move into it. You define variables as much as you like. After calculation, you can exit the pry session with exit.