Interactive mode

Discussion in 'Ruby' started by Rick Ashton, Jul 17, 2006.

  1. Rick Ashton

    Rick Ashton Guest

    Hi

    Is there some sort of 'python -i' equivalent for ruby or irb? I can't
    seem to find it. I know you can 'load' or 'require' a module, but I need
    to load the file into the current scope. Currently all I seem to be able
    to do is either code and run file, or copy and paste into irb if I
    wanted the '-i' functionality. I'd really like to "enter interactive
    mode after executing the script or the command" as the python man page
    puts it. It'll be enormously useful for testing and stuff. Sorry if it's
    a rather n00b or repeated question, I couldn't search since the function
    is "disabled due to technical problems".

    Thanks!

    --
    Posted via http://www.ruby-forum.com/.
     
    Rick Ashton, Jul 17, 2006
    #1
    1. Advertising

  2. On 7/17/06, Nicholas Seckar <> wrote:
    > If you just want to load some code before irb starts, you can use -r
    >
    > cd /tmp
    > echo 'puts 10' > startup.rb
    > irb -r startup.rb
    >
    > If you'd like to stop execution in various places, take a look at
    > ruby-breakpoint.
    >
    > On 7/17/06, Rick Ashton <> wrote:
    > >
    > > Hi
    > >
    > > Is there some sort of 'python -i' equivalent for ruby or irb? I can't
    > > seem to find it. I know you can 'load' or 'require' a module, but I need
    > > to load the file into the current scope. Currently all I seem to be able
    > > to do is either code and run file, or copy and paste into irb if I
    > > wanted the '-i' functionality. I'd really like to "enter interactive
    > > mode after executing the script or the command" as the python man page
    > > puts it. It'll be enormously useful for testing and stuff. Sorry if it's
    > > a rather n00b or repeated question, I couldn't search since the function
    > > is "disabled due to technical problems".
    > >
    > > Thanks!
    > >
    > > --
    > > Posted via http://www.ruby-forum.com/.
    > >
    > >

    >
    >


    As mentioned you can use breakpoint (excellect tool) and irb -r, also
    don't forget you can use require and/or load from within irb as well
    to load (and execute) a script.

    pth
     
    Patrick Hurley, Jul 17, 2006
    #2
    1. Advertising

  3. Rick Ashton

    Rick Ashton Guest

    Thanks guys

    But it still doesn't load the file into the current scope as I mentioned
    before. Is there some way to do this?

    ie:

    $ echo a=5 > blah.rb
    $ irb -r blah.rb
    irb(main):001:0> a
    NameError: undefined local variable or method `a' for main:Object
    from (irb):1


    Thanks!

    --
    Posted via http://www.ruby-forum.com/.
     
    Rick Ashton, Jul 17, 2006
    #3
  4. Rick Ashton

    Mike Harris Guest

    I think he wants to be able to have a file with abc=4 in it, and then be
    able to load the file and have the local variable abc be in scope.
    Neither of these solutions do that.

    Patrick Hurley wrote:

    > On 7/17/06, Nicholas Seckar <> wrote:
    >
    >> If you just want to load some code before irb starts, you can use -r
    >>
    >> cd /tmp
    >> echo 'puts 10' > startup.rb
    >> irb -r startup.rb
    >>
    >> If you'd like to stop execution in various places, take a look at
    >> ruby-breakpoint.
    >>
    >> On 7/17/06, Rick Ashton <> wrote:
    >> >
    >> > Hi
    >> >
    >> > Is there some sort of 'python -i' equivalent for ruby or irb? I can't
    >> > seem to find it. I know you can 'load' or 'require' a module, but I

    >> need
    >> > to load the file into the current scope. Currently all I seem to be

    >> able
    >> > to do is either code and run file, or copy and paste into irb if I
    >> > wanted the '-i' functionality. I'd really like to "enter interactive
    >> > mode after executing the script or the command" as the python man

    >> page
    >> > puts it. It'll be enormously useful for testing and stuff. Sorry if

    >> it's
    >> > a rather n00b or repeated question, I couldn't search since the

    >> function
    >> > is "disabled due to technical problems".
    >> >
    >> > Thanks!
    >> >
    >> > --
    >> > Posted via http://www.ruby-forum.com/.
    >> >
    >> >

    >>
    >>

    >
    > As mentioned you can use breakpoint (excellect tool) and irb -r, also
    > don't forget you can use require and/or load from within irb as well
    > to load (and execute) a script.
    >
    > pth
    >
    >
     
    Mike Harris, Jul 17, 2006
    #4
  5. Rick Ashton schrieb:
    > Thanks guys
    >
    > But it still doesn't load the file into the current scope as I mentioned
    > before. Is there some way to do this?
    >
    > ie:
    >
    > $ echo a=5 > blah.rb
    > $ irb -r blah.rb
    > irb(main):001:0> a
    > NameError: undefined local variable or method `a' for main:Object
    > from (irb):1
    >
    >
    > Thanks!
    >


    Hi Rick,

    try eval(File.read('blah.rb'))

    ~dingsi
     
    Thorben Mueller, Jul 17, 2006
    #5
  6. On 7/17/06, Thorben Mueller <> wrote:
    > Rick Ashton schrieb:
    > > Thanks guys
    > >
    > > But it still doesn't load the file into the current scope as I mentioned
    > > before. Is there some way to do this?
    > >
    > > ie:
    > >
    > > $ echo a=5 > blah.rb
    > > $ irb -r blah.rb
    > > irb(main):001:0> a
    > > NameError: undefined local variable or method `a' for main:Object
    > > from (irb):1
    > >
    > >
    > > Thanks!
    > >

    >
    > Hi Rick,
    >
    > try eval(File.read('blah.rb'))
    >
    > ~dingsi
    >
    >


    eval() won't create local variables - it will update one if it already
    exists however:

    $ cat eval-test.rb
    a = 42
    $ cat eval-scope.rb
    eval(File.read("eval-test.rb"))
    p a
    $ ruby eval-scope.rb
    eval-scope.rb:2: undefined local variable or method `a' for
    main:Object (NameError)

    $ cat eval-scope2.rb
    a = nil
    eval(File.read("eval-test.rb"))
    p a
    $ ruby eval-scope2.rb
    42


    Regards,
    Sean
     
    Sean O'Halpin, Jul 17, 2006
    #6
  7. Sean O'Halpin wrote:
    > On 7/17/06, Thorben Mueller <> wrote:
    >> Rick Ashton schrieb:
    >> > Thanks guys
    >> >
    >> > But it still doesn't load the file into the current scope as I

    >> mentioned
    >> > before. Is there some way to do this?
    >> >
    >> > ie:
    >> >
    >> > $ echo a=5 > blah.rb
    >> > $ irb -r blah.rb
    >> > irb(main):001:0> a
    >> > NameError: undefined local variable or method `a' for main:Object
    >> > from (irb):1
    >> >
    >> >
    >> > Thanks!
    >> >

    >>
    >> Hi Rick,
    >>
    >> try eval(File.read('blah.rb'))
    >>
    >> ~dingsi
    >>
    >>

    >
    > eval() won't create local variables - it will update one if it already
    > exists however:
    >
    > $ cat eval-test.rb
    > a = 42
    > $ cat eval-scope.rb
    > eval(File.read("eval-test.rb"))
    > p a
    > $ ruby eval-scope.rb
    > eval-scope.rb:2: undefined local variable or method `a' for
    > main:Object (NameError)
    >
    > $ cat eval-scope2.rb
    > a = nil
    > eval(File.read("eval-test.rb"))
    > p a
    > $ ruby eval-scope2.rb
    > 42
    >
    >
    > Regards,
    > Sean
    >


    Interestingly, it _does_ work for me within irb:

    $ cat test.rb
    puts "hi"
    a = 1
    $ irb
    irb(main):001:0> eval(File.read('test.rb'))
    hi
    => 1
    irb(main):002:0> puts a
    1
    => nil



    Which I think will work for what the OP wanted?

    -Justin
     
    Justin Collins, Jul 18, 2006
    #7
  8. On 7/18/06, Justin Collins <> wrote:
    >
    >
    > Sean O'Halpin wrote:
    > > On 7/17/06, Thorben Mueller <> wrote:
    > >> Rick Ashton schrieb:
    > >> > Thanks guys
    > >> >
    > >> > But it still doesn't load the file into the current scope as I
    > >> mentioned
    > >> > before. Is there some way to do this?
    > >> >
    > >> > ie:
    > >> >
    > >> > $ echo a=5 > blah.rb
    > >> > $ irb -r blah.rb
    > >> > irb(main):001:0> a
    > >> > NameError: undefined local variable or method `a' for main:Object
    > >> > from (irb):1
    > >> >
    > >> >
    > >> > Thanks!
    > >> >
    > >>
    > >> Hi Rick,
    > >>
    > >> try eval(File.read('blah.rb'))
    > >>
    > >> ~dingsi
    > >>
    > >>

    > >
    > > eval() won't create local variables - it will update one if it already
    > > exists however:
    > >
    > > $ cat eval-test.rb
    > > a = 42
    > > $ cat eval-scope.rb
    > > eval(File.read("eval-test.rb"))
    > > p a
    > > $ ruby eval-scope.rb
    > > eval-scope.rb:2: undefined local variable or method `a' for
    > > main:Object (NameError)
    > >
    > > $ cat eval-scope2.rb
    > > a = nil
    > > eval(File.read("eval-test.rb"))
    > > p a
    > > $ ruby eval-scope2.rb
    > > 42
    > >
    > >
    > > Regards,
    > > Sean
    > >

    >
    > Interestingly, it _does_ work for me within irb:
    >
    > $ cat test.rb
    > puts "hi"
    > a = 1
    > $ irb
    > irb(main):001:0> eval(File.read('test.rb'))
    > hi
    > => 1
    > irb(main):002:0> puts a
    > 1
    > => nil
    >
    >
    >
    > Which I think will work for what the OP wanted?
    >
    > -Justin
    >
    >

    You're right - another one of those little quirks of irb ;)

    (I'm too abashed to admit I missed the point)

    Regards,
    Sean
     
    Sean O'Halpin, Jul 18, 2006
    #8
  9. On Jul 17, 2006, at 7:12 PM, Sean O'Halpin wrote:

    > On 7/18/06, Justin Collins <> wrote:
    >>
    >>
    >> Sean O'Halpin wrote:
    >> > On 7/17/06, Thorben Mueller <> wrote:
    >> >> Rick Ashton schrieb:
    >> >> > Thanks guys
    >> >> >
    >> >> > But it still doesn't load the file into the current scope as I
    >> >> mentioned
    >> >> > before. Is there some way to do this?
    >> >> >
    >> >> > ie:
    >> >> >
    >> >> > $ echo a=5 > blah.rb
    >> >> > $ irb -r blah.rb
    >> >> > irb(main):001:0> a
    >> >> > NameError: undefined local variable or method `a' for

    >> main:Object
    >> >> > from (irb):1
    >> >> >
    >> >> >
    >> >> > Thanks!
    >> >> >
    >> >>
    >> >> Hi Rick,
    >> >>
    >> >> try eval(File.read('blah.rb'))
    >> >>
    >> >> ~dingsi
    >> >>
    >> >>
    >> >
    >> > eval() won't create local variables - it will update one if it

    >> already
    >> > exists however:
    >> >
    >> > $ cat eval-test.rb
    >> > a = 42
    >> > $ cat eval-scope.rb
    >> > eval(File.read("eval-test.rb"))
    >> > p a
    >> > $ ruby eval-scope.rb
    >> > eval-scope.rb:2: undefined local variable or method `a' for
    >> > main:Object (NameError)
    >> >
    >> > $ cat eval-scope2.rb
    >> > a = nil
    >> > eval(File.read("eval-test.rb"))
    >> > p a
    >> > $ ruby eval-scope2.rb
    >> > 42
    >> >
    >> >
    >> > Regards,
    >> > Sean
    >> >

    >>
    >> Interestingly, it _does_ work for me within irb:
    >>
    >> $ cat test.rb
    >> puts "hi"
    >> a = 1
    >> $ irb
    >> irb(main):001:0> eval(File.read('test.rb'))
    >> hi
    >> => 1
    >> irb(main):002:0> puts a
    >> 1
    >> => nil
    >>
    >>
    >>
    >> Which I think will work for what the OP wanted?
    >>
    >> -Justin
    >>
    >>

    > You're right - another one of those little quirks of irb ;)
    >
    > (I'm too abashed to admit I missed the point)
    >
    > Regards,
    > Sean
    >


    It's not _really_ a little quirk of irb, it's the same quirk you
    noted of eval (Since irb uses #eval to well, evaluate ;) ).

    e.g.:

    % cat demo_eval.rb
    puts eval("x = 1")
    puts begin
    x
    rescue
    "At parse time, x has not been declared as a local variable (by
    assigning to it.)\n" +
    "This means it will raise an exception when it gets evaluated here."
    end

    puts eval("x") # But x does indeed exist.


    % ruby demo_eval.rb
    1
    At parse time, x has not been declared as a local variable (by
    assigning to it.)
    This means it will raise an exception when it gets evaluated here.
    1
     
    Logan Capaldo, Jul 18, 2006
    #9
  10. On 7/18/06, Logan Capaldo <> wrote:
    >
    > On Jul 17, 2006, at 7:12 PM, Sean O'Halpin wrote:
    >
    > > On 7/18/06, Justin Collins <> wrote:
    > >>
    > >>
    > >> Sean O'Halpin wrote:
    > >> > On 7/17/06, Thorben Mueller <> wrote:
    > >> >> Rick Ashton schrieb:
    > >> >> > Thanks guys
    > >> >> >
    > >> >> > But it still doesn't load the file into the current scope as I
    > >> >> mentioned
    > >> >> > before. Is there some way to do this?
    > >> >> >
    > >> >> > ie:
    > >> >> >
    > >> >> > $ echo a=5 > blah.rb
    > >> >> > $ irb -r blah.rb
    > >> >> > irb(main):001:0> a
    > >> >> > NameError: undefined local variable or method `a' for
    > >> main:Object
    > >> >> > from (irb):1
    > >> >> >
    > >> >> >
    > >> >> > Thanks!
    > >> >> >
    > >> >>
    > >> >> Hi Rick,
    > >> >>
    > >> >> try eval(File.read('blah.rb'))
    > >> >>
    > >> >> ~dingsi
    > >> >>
    > >> >>
    > >> >
    > >> > eval() won't create local variables - it will update one if it
    > >> already
    > >> > exists however:
    > >> >
    > >> > $ cat eval-test.rb
    > >> > a = 42
    > >> > $ cat eval-scope.rb
    > >> > eval(File.read("eval-test.rb"))
    > >> > p a
    > >> > $ ruby eval-scope.rb
    > >> > eval-scope.rb:2: undefined local variable or method `a' for
    > >> > main:Object (NameError)
    > >> >
    > >> > $ cat eval-scope2.rb
    > >> > a = nil
    > >> > eval(File.read("eval-test.rb"))
    > >> > p a
    > >> > $ ruby eval-scope2.rb
    > >> > 42
    > >> >
    > >> >
    > >> > Regards,
    > >> > Sean
    > >> >
    > >>
    > >> Interestingly, it _does_ work for me within irb:
    > >>
    > >> $ cat test.rb
    > >> puts "hi"
    > >> a = 1
    > >> $ irb
    > >> irb(main):001:0> eval(File.read('test.rb'))
    > >> hi
    > >> => 1
    > >> irb(main):002:0> puts a
    > >> 1
    > >> => nil
    > >>
    > >>
    > >>
    > >> Which I think will work for what the OP wanted?
    > >>
    > >> -Justin
    > >>
    > >>

    > > You're right - another one of those little quirks of irb ;)
    > >
    > > (I'm too abashed to admit I missed the point)
    > >
    > > Regards,
    > > Sean
    > >

    >
    > It's not _really_ a little quirk of irb, it's the same quirk you
    > noted of eval (Since irb uses #eval to well, evaluate ;) ).
    >
    > e.g.:
    >
    > % cat demo_eval.rb
    > puts eval("x = 1")
    > puts begin
    > x
    > rescue
    > "At parse time, x has not been declared as a local variable (by
    > assigning to it.)\n" +
    > "This means it will raise an exception when it gets evaluated here."
    > end
    >
    > puts eval("x") # But x does indeed exist.
    >
    >
    > % ruby demo_eval.rb
    > 1
    > At parse time, x has not been declared as a local variable (by
    > assigning to it.)
    > This means it will raise an exception when it gets evaluated here.
    > 1
    >

    It's even quirkier than that even :)

    # (ruby-talk 202571)
    puts eval("x = 1")
    puts begin
    x
    rescue
    "At parse time, x has not been declared as a local variable (by
    assigning to it.)\n" +
    "This means it will raise an exception when it gets evaluated here."
    end

    puts "But x does indeed exist."
    puts eval("x")
    begin
    puts x
    rescue
    puts "or does it?"
    end
    p local_variables
    __END__
    1
    At parse time, x has not been declared as a local variable (by
    assigning to it.)
    This means it will raise an exception when it gets evaluated here.
    1
    or does it?
    ["x"]

    My point was that eval won't introduce local variables into the
    current scope (which irb does because of the tricks it plays with
    bindings). It appears that when eval is called without a binding, it
    temporarily pushes the current frame, re-uses the binding in effect
    when the eval was called, then pops the current frame back again which
    I guess has the effect of erasing changes to local variables (but
    someone should tell the local_variables method!). This creates the
    effect of a kind of binding that persists between calls to eval but
    which is inaccessible to the local scope (except via eval). At least,
    that's how it seems to me. I'd be grateful for a definitive
    explanation of what's going on in eval.c:6500ff.

    Regards,
    Sean
     
    Sean O'Halpin, Jul 18, 2006
    #10
  11. Rick Ashton

    Rick Ashton Guest

    Hey!

    Thanks guys!

    eval(File.read('blah.rb')) in irb does the trick! Though it is rather a
    quirky way things are handled:) I've moved over from python and I'm
    wondering how people do without this simple feature? I find it
    invaluable in test-running my app since I can run it as a whole, still
    inspect things, and modify things live as the app is being run. The
    alternative to use irb as interactive ruby is great, but it doesn't
    allow me to import existing code easily (until now:) and I must retype
    everything if I wanted to test it. It's not necessarily for debugging an
    app (ruby's got a cool debugger for that:), but testing out features and
    functions interactively (what an interactive shell prompt allows us to
    do) without having to retype everything into irb. What do you guys use
    normally for something like this?

    I think ruby/irb -r debug also works but how do I get it to stop
    executing after the last line? I can't quite set a breakpoint there.
    Will have a look at ruby-breakpoint too thanks! :)

    --
    Posted via http://www.ruby-forum.com/.
     
    Rick Ashton, Jul 19, 2006
    #11
    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. Alex
    Replies:
    1
    Views:
    319
    Peter Hansen
    Sep 9, 2005
  2. John J Lee
    Replies:
    3
    Views:
    487
    bruno at modulix
    Dec 1, 2005
  3. Edward Loper
    Replies:
    0
    Views:
    472
    Edward Loper
    Aug 7, 2007
  4. John J Lee
    Replies:
    0
    Views:
    526
    John J Lee
    Aug 7, 2007
  5. Edward Loper

    mmm-mode, python-mode and doctest-mode?

    Edward Loper, Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    437
    Edward Loper
    Aug 9, 2007
Loading...

Share This Page