1

This has been a surprisingly hard question to find an answer to.

(A few questions seem to be asking something at least similar, like:

Ruby console alternative for IRB (Windows)

IDLE like interactive console for Ruby

A recommended Ruby interactive console

But I couldn't get what I need from any of those.)

Also, I'm a bit unsure of the precise terminology I should be using, so I'll try to be really concrete here:

Say you're using IDLE with the Python shell.

You have one of IDLE's text-editor windows open with a script "example.py" in it.

You hit F5 and the Python shell comes up and does exactly what it would do if you had just entered every line in "example.py" into the shell line-by-line.

Functionally, that's exactly what it's doing is automatically entering every line, only without cluttering up the screen by displaying them. (Also it resets the shell to a fresh state every time you do this, but that's not really the important point at the moment; Sometimes it'd be nice to have the option of having it not reset the state of the shell, whatever.)

So the outcome is that now you can play around in the shell, and all the functions and variables etc that were in the script that you just ran are all there.

But with irb...

How do I get the same effect?

For instance, I tried irb example.rb (an equivalent ruby script) in the Windows console, and it just literally enters each line one-by-one into irb, spewing them down the screen, then automatically exits back to the Windows command prompt.

(Although even if that did work the way I wanted (is there some option flaggy argument thingy that would make it do more what I want here?), I'd still have to alt-tab from the text-editor to a console window, and enter the command and file name, which is inferior to just pressing F5, obvs.)

To make really sure I'm being clear in what I mean, here are concrete examples of:

1) a Python script for "example.py" 2) an example of running it in the shell then doing some things in the shell (copy-pasted from the actual shell)

3) an equivalent Ruby script to that Python one 4) an example of running it in the kludgy, slow online-interpreter at repl.it, and doing the exact same things in that irb shell (again, copy-pasted)

1) example.py :

x = "some value you don't want to keep reassigning to this variable"
y = "some other value like that"

def some_function(var):
    return "do something complicated with `"+var+"`"


print("example.py just ran")

2) Python shell :

Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
example.py just ran
>>> x
"some value you don't want to keep reassigning to this variable"
>>> y
'some other value like that'
>>> print(some_function(x))
do something complicated with `some value you don't want to keep reassigning to this variable`
>>> x = "a frog"
>>> print(some_function(x))
do something complicated with `a frog`
>>> print("gonna run example.py again")
gonna run example.py again
>>> ================================ RESTART ================================
>>> 
example.py just ran
>>> print("x is back to: `\""+x+"\"`")
x is back to: `"some value you don't want to keep reassigning to this variable"`

3) example.rb :

x = "some value you don't want to keep reassigning to this variable"
y = "some other value like that"

def some_function var
        "do something complicated with `#{var}`"
end

puts "test.rb just ran"

4) online Ruby irb shell thing at repl.it :

Ruby 1.8.7 (2008-05-31 patchlevel 0) [x86-linux]
[GCC 4.2.1 (LLVM, Emscripten 1.5, Emscripted-Ruby)]

test.rb just ran
=> nil
   x
=> "some value you don't want to keep reassigning to this variable"
   y
=> "some other value like that"
   puts some_function x
do something complicated with `some value you don't want to keep reassigning to this variable`
=> nil
   x = "a frog"
=> "a frog"
   puts some_function x
do something complicated with `a frog`
=> nil
   puts "gonna run this script again..."
gonna run this script again...
=> nil

test.rb just ran
=> nil
   puts "x is back to: `\"#{x}\"`"
x is back to: `"some value you don't want to keep reassigning to this variable"`
=> nil
Community
  • 1
  • 1
Owen_AR
  • 2,867
  • 5
  • 20
  • 23
  • 1
    First: you should trim down your question. It's way too long. Second: are you looking for a Ruby IDE (Integrated Development Environment) where you can edit code, manage projects files, and run/debug all in one program? – Reinstate Monica -- notmaynard Apr 16 '13 at 17:10
  • Yeah, it did somehow end up really long... I happen to be right at the edge "too exhausted to function" atm, so I'll trim it down later. What I want is exactly what I said, though: The thing that is common to both 1)2) and 2)3). 1) example Python script. 2) able to do stuff like [example] with IDLE and Python shell. -----> 3) equivalent example Ruby script 4) able to do stuff like [equivalent example] with Ruby and repl.it interpreter online. So I basically want something that works exactly like that repl.it thing, only... not online and slow and a ridiculously spartan text-editor. – Owen_AR Apr 16 '13 at 17:27

2 Answers2

2

The best answer I've found so far seems to be to install pry (enter gem install pry at the command prompt).

Now if you run a script by entering pry scriptname.rb, and it encounters an exception, it will automatically go interactive.

Furthermore, if you add require 'pry' inside the script at the top, then you can put binding.pry in the script at ay point.

Now when you run the script (simply by entering scriptname.rb at the command prompt), it will stop executing and go interactive in pry at that point.

Pressing ^D (ie, ctrl-d) will resume execution.

Getting a script to run in a command prompt window when you press F5 (or whatever) in your editor (eg, Notepad++) is apparently somewhat trickier. Solutions like this have some problems that I haven't figure out how to tweak away yet.

So currently I'm just alt-tabbing to the command prompt window and running the script from there (again, by entering pry scriptname.rb or just scriptname.rb, depending on what exactly what I want and whether I put a binding.pry in the script anywhere. Up-arrow recall, tab completion, blah blah.)

I'm working on figuring it out.

Community
  • 1
  • 1
Owen_AR
  • 2,867
  • 5
  • 20
  • 23
0

I am not 100% sure if this is what you want, but from my experience, using ruby scriptname.rb will run the code

Mining15
  • 83
  • 1
  • 6