Ruby: How to Load a File into Interactive Ruby Console (Irb)

How to create custom IRB file to load a Ruby project's files, gems and dependencies?

Very simple, actually:

#!/usr/bin/env ruby
require "bundler/setup"
# ...
# everything else you need
# ...
require "irb"
IRB.start

When you start IRB using IRB.start, you will have available everything that's been loaded/initialised before it.

How to use open() in irb console to print out basic text from .txt file? #Ruby

I have created a file ex15_sample.txt in .../Ruby/zintlist/irb.

1.8.6 :082 > File.open("ex15_sample.txt")
Errno::ENOENT: No such file or directory - ex15_sample.txt
from (irb):82:in `initialize'
from (irb):82:in `open'
from (irb):82
from :0
1.8.6 :086 > Dir.getwd
=> "/.../Ruby/prod/spec"
1.8.6 :087 > Dir.chdir('../../zintlist/irb')
=> 0
1.8.6 :088 > Dir.getwd
=> "/.../Ruby/zintlist/irb"
1.8.6 :089 > File.open("ex15_sample.txt")
=> #<File:ex15_sample.txt>
1.8.6 :090 >

attempting File.open("ex15_sample.txt") I assume it opens

Within irb, usually you don't need to assume, you have an immediate answer.

1.8.6 :090 > txt = File.open("ex15_sample.txt")
=> #<File:ex15_sample.txt>
1.8.6 :091 > puts txt.read()
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
=> nil
1.8.6 :092 >

Can you 'require' ruby file in irb session, automatically, on every command?

I usually create a simple function like this:

def reload
load 'myscript.rb'
# Load any other necessary files here ...
end

With that, a simple reload will re-import all of the scripts that I'm working on. It's not automatic, but it's the closest thing that I've been able to come up with.

You may be able to override method_missing to call this function automatically when your object is invoked with a method that doesn't exist. I've never tried it myself, though, so I can't give any specific advice. It also wouldn't help if you're calling a method that already exists but has simply been modified.

In my own laziness, I've gone as far as mapping one of the programmable buttons on my mouse to the key sequence "reload<enter>". When I'm using irb, all it takes is the twitch of a pinky finger to reload everything. Consequently when I'm not using irb, I end up with the string "reload" inserted in documents unintentionally (but that's a different problem entirely).

How to load self-written ruby function into Ruby interactive consoles(irb)?

From the interpreter:

load('my_file.rb')

Execute program file with irb and remain interactive

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)>

IRb: how to start an interactive ruby session with pre-loaded classes

I'm not sure it's possible to 'flush' a session. However, you can load your classes like this:

irb -r 'hello.rb' -r 'hello_objects.rb'


Related Topics



Leave a reply



Submit