Why does IRB not ignore EOF as per IRB.conf hash - ruby

I'm trying to start IRB and run a file, foo.rb, in one command,
irb foo.rb
When foo.rb is done I want another IRB prompt. Instead, it prints an IRB prompt, then exits.
I checked the IRB docs and I changed IRB.conf[:IGNORE_EOF] = true. I confirmed that hash value inside IRB. Is the behavior I want set by this hash? If so, what else could I be doing wrong?

The docs for irb say about that configuration:
**conf.ignore_eof = true/false**
Whether ^D (control-d) will be ignored or not. If false is set, ^D means quit.
So, no that setting isn't meant to do what you're looking for. As far as I can tell, there isn't a way to do what you want with irb. The closest would be to start irb without an argument, then use require './foo.rb' to run that file.

Related

Execute program file with irb and remain interactive

Is it possible to run irb, pass it a .rb file that gets automatically executed, and have irb stay running in interactive mode?
On Windows, I have a file called checkme.bat that basically does the following:
irb checkme.rb
The problem right now is that when I run checkme.bat, irb executes the program file but then simply exits, leaving me back at the command line.
One way to do this is to use pry and add the line binding.pry at the point in your script where you want to bail out into the repl.
x = 3
puts "Hello!"
binding.pry
Then you can run your script with pry and it'll let you examine what's going on.
~> pry d.rb
Hello
[1] pry(main)> x
=> 3
[2] pry(main)>
I'm not quite sure but on Windows when you execute a .rb file it instantly closes, try using gets at the end of the program.

Ruby, pry: Can I add something to the command `pry example.rb` so pry automatically goes interactive when it finishes executing the script?

Pry goes into interactive mode if it encounters an exception (eg if you just put an undefined variable 'x' at the end of the script).
(Also if, inside the script itself you require 'pry' and put binding.pry at the point you want to go interactive at.)
But I'm wondering: Is there's some kind of flag/option/argument thingy that I can add to the pry example.rb command when I enter it at the command prompt, so it will go interactive when it reaches the end of executing any example.rb script, regardless of what's inside? (Assuming no exceptions before the end, of course.)
(This would obviously be especially useful for use with editors that you can run external programs from like Notepad++, see this and this.)
Not yet, but file an issue and i'll add it :)

How can I pass STDin to IRB without it exiting after processing this input?

I'm using a short bash script to help me test an implementation of linked lists in ruby for class. I know about rspec and unit testing, and I'm certain they're better options for what I'm trying to do, but I was able to figure out this command
echo "require './nodes'" | irb
The output afterwards is
Switch to inspect mode.
require './nodes'
true
Technically a success, but the irb process ends there. So I tried
echo "require './nodes'" | irb --noinspect
Which gave me
Switch to non inspect mode.
require './nodes'
true
And it again exits the irb process.
I'm just trying to make my workflow a little bit more convenient, as I like to use irb to test my files by poking around at them and seeing what happens.
Create a simple script, the code below will land you into the irb shell with the 'nodes' gem included. If you are using Ruby 1.8.x then you'll need to add require 'rubygems' before requiring nodes gem
#!/path/to/ruby -w
require 'irb'
require 'nodes'
IRB.start

How can I use irb the Ruby interpreter to test/debug my .rb files?

How can I use irb the Ruby interpreter to test/debug my .rb files? I want to load an .rb file, let it run, and after it ends, get back to the irb prompt so I would be able to manipulate the variables which my script has built.
I tried load, require, and irb -r, but none of them worked as I would like. After the execution, when the program terminates, I get an irb prompt, but all of the variables are inaccessible. What can I do?
Not sure exactly what you want to do, but it sounds like you may want to use the "pry" gem instead.
Install the pry gem see: http://pryrepl.org
require 'pry' at the start of your program.
put binding.pry at the end of your program (or where you want to start the interactive session)
run your program.
Using pry you will have all your variables in scope.
More information see the link above, and here:
http://banisterfiend.wordpress.com/2011/01/27/turning-irb-on-its-head-with-pry/
and:
https://github.com/pry/pry/wiki/Runtime-invocation
If the variables you want are local variables, I do not think there is a way you can access them from another file.
If you just want the return value of the whole code that is on a different file, you can eval the whole content of that file within the main code and access the return value. You can access multiple values by putting them in an array or a hash at the end of the file to be evaled.
Add following code in place where you want irb to start
require 'irb'
IRB.start(__FILE__)
binding.irb
As of Ruby 2.4, placing this in your code will drop you into an irb session in the scope where it is placed.
No additional gem installs or require statements are needed.

dropping user to IRB after reading from pipe

I have this script that drops the user to an IRB session when executed.
All good, but when I use *nix pipes to get the input (e.g. with cat), the IRB session ends immediately.
I could reduce the script (let's call it myscript.rb) to the following:
require 'irb'
if $stdin.stat.size > 0
#text = $stdin.read
else
#text= "nothing"
end
ARGV.clear
IRB.start
When executed like: ruby myscript.rb, I end up in the IRB session (as expected).
But (assuming foo.txt exists in the cwd): cat foo.txt | ruby myscript.rb will just print the IRB prompt and then the IRB session is closed (I'm being dropped to $bash).
Any known workarounds or ideas?
BTW: it has the same behavior on ruby 1.8.7 as well as on 1.9.2.
I think your problem is that when you pipe to your script STDIN will be the stream from your file, so when when you start IRB it will read from the same stream, but notice that it's at its end, and quit, just like it would when you type ctrl-D (which is a manual end of file signal).
You can reopen STDIN to read from the tty (i.e. the keyboard) like this:
STDIN.reopen(File.open('/dev/tty', 'r'))
but it looks a bit weird for me, I don't get the proper IRB promt. IRB works though.
#Theo identified the problem.
Also, requiring irb before IRB.start will fix missing IRB settings. In the end, the code looks like this:
if $stdin.stat.size > 0
#text = $stdin.read
$stdin.reopen(File.open("/dev/tty", "r"))
else
#text= "nothing"
end
require 'irb'
ARGV.clear
IRB.start
$stdin.read reads your input before IRB has a chance to read it (if you're trying to force IRB to execute commands from stdin).

Resources