# Irb practice. Autoloading files

**URL:** <https://www.ruby-forum.com/t/irb-practice-autoloading-files/128869>\
**Category:** Ruby\
**Created:** [February 12, 2008, 10:17am UTC](https://www.ruby-forum.com/t/irb-practice-autoloading-files/128869 "2008-02-12T10:17:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 12, 2008, 10:17am UTC](https://www.ruby-forum.com/t/irb-practice-autoloading-files/128869/1 "2008-02-12T10:17:51Z")

</div>

I often found my self retyping a lot of lines when experimenting or  
testing in irb after making some changes. I suppose I should be able to  
avoid it with a better planning, but…

Anyhow, I wrote a function in my .irbrc that reloads the listed file and  
its dependencies so that I can work on my text editor (btw, I’m using  
vim) and have irb automatically rerun my script after I made my changes  
(in the spirit of Zen test’s autotest). Something like this:

def auto\_load main, \*dependencies  
Thread.new do  
main\_mtime = File.mtime main; load main  
dep\_mtime = {}  
dependencies.each {|f| dep\_mtime[f] = File.mtime f; load f }  
loop do  
change = false  
dependencies.each do |f|  
if (t = File.mtime f) \> dep\_mtime[f]  
change = true; dep\_mtime[f] = t; load f  
end  
end  
if (t = File.mtime main) \> main\_mtime or change  
main\_mtime = t; load main  
end  
sleep 1  
end.run  
end

It’s rather crude, perhaps even a clumsy irb practice. I thought maybe  
other people have the same need and probably with a better solution.  
Does anybody have a suggestion for a better solution or a better  
practice when experimenting or testing with irb? Is there a ruby gem  
that would be helpful?

You may say “what’s wrong with writing in test.rb and retyping load  
‘test.rb’ after making changes?”. Well, it’s kind of nice to automate  
that, plus if the change is in one of the ‘require’d files, I need to  
re’load’ that.

-andre

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 12, 2008, 1:08pm UTC](https://www.ruby-forum.com/t/irb-practice-autoloading-files/128869/2 "2008-02-12T13:08:21Z")

</div>

Andreas S wrote:

> I often found my self retyping a lot of lines when experimenting or  
> testing in irb after making some changes.

So why not just never use irb? Instead, open up vim to my\_tests.rb and  
write all the practice code you want. It will still be there after you  
run the program.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 11, 2008, 6:45am UTC](https://www.ruby-forum.com/t/irb-practice-autoloading-files/128869/3 "2008-03-11T06:45:50Z")

</div>

> You may say “what’s wrong with writing in test.rb and retyping load ‘test.rb’ after making changes?”. Well, it’s kind of nice to automate that, plus if the change is in one of the ‘require’d files, I need to re’load’ that.

Thanks a lot for this code! I started thinking the same as you - why  
do I have to manually reload the file I changed? (yes, irb has a  
history that can be accessed by pressing the up arrow key or searched  
by Ctrl-R, but it is none the less manual labor). Perhaps there is a  
more beatiful solution to this, but until I find that I will use your  
solution! Thanks!

Best regards,  
Fredrik
