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

12,209

Solution 1

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

Solution 2

This won't run every command, but you can include a file on every IRb session. ~/.irbrc is loaded each time you start an IRb session.

~/.irbrc

require "~/somefile.rb"

~/somefile.rb

puts "somefile loaded"

terminal

> irb
somefile loaded
irb(main):001:0> 

~/.irbrc is loaded each time you start an irb session

Solution 3

What about require_dependency from ActiveSupport library?

require 'active_support/dependencies' #load it at the beginning
require_dependency 'myscript.rb'

Then require_dependency should track the changes in myscript file and reload it.

Share:
12,209
Damien Roche
Author by

Damien Roche

Working on.. https://moonfactor.com. Cryptocurrency tracker and collaborative research tool for discovering potential ‘moonshots’ — low market cap assets with high risk/reward. Originally a hack project for personal use, now has a small community. Stackweaver. Edu platform for full-stack web development. This is a huge multi-year project. The initial release will contain a dev buddy matchmaking system to help people find others to learn and collaborate with. The matchmaker is mostly done but nothing has been deployed yet. Planned for next year will be development of the project-based curriculum. Current tech stack: Vue/TypeScript, Kotlin, Ruby, Go, Kubernetes, PostgreSQL, Elastic Search, RabbitMQ, Redis, Mongo, maybe Kafka in the future.. too many bloody toys! Background: I’ve been in web development since around 2008 working across frontend and backend, mostly freelancing but had a full-time job for a few years. Currently travelling, contracting and building side projects.

Updated on June 29, 2022

Comments

  • Damien Roche
    Damien Roche almost 2 years

    I am currently editing a file, and I'm using irb to test the api:

    > require './file.rb'
    > o = Object.new
    > o.method
    

    I then want to be able to edit the file.rb, and be able to see changes immediately. Example: assume new_method did not exist when I first required file.rb:

    > o.new_method
    

    Which will return an error. Is there a sandbox/developer mode or a method whereby I can achieve the above without having to reload the file every time? Require will not work after the first require, regardless. I assume worst case I'd have to use load instead.

  • Andrew Marshall
    Andrew Marshall almost 12 years
    As a note, Rails provides reload! that does essentially the same thing.
  • Damien Roche
    Damien Roche almost 12 years
    @AndrewMarshall we are talking about irb, not Rails.
  • Andrew Marshall
    Andrew Marshall almost 12 years
    @Zenph I know… that's why it was a "note", and reinforces that this is a somewhat common way to do this sort of thing.
  • Andrew Marshall
    Andrew Marshall almost 12 years
    Btw, using method_missing is a clever but evil idea. Of course it doesn't solve the problem where a method changed.
  • Damien Roche
    Damien Roche almost 12 years
    @AndrewMarshall sorry. Thanks. If I hadn't known about Rails console reload! method I'd be a very happy chappy right now.
  • Damien Roche
    Damien Roche almost 12 years
    Is there not perhaps a more improved version of irb, or a wrapper, which offers this functionality? I like method_missing response, but like you say, won't work with modified methods.
  • Damien Roche
    Damien Roche almost 12 years
    @AndrewMarshall Thanks, I'll give it a try.
  • megas
    megas almost 12 years
    I've tested it and unfortunately it didn't work, maybe someone else can make it work.