Search code examples
rubyirb

how does irb require work?


Typically when I am testing small snippets of code for Ruby, I many times put code chunks in separate files in the same directory, run irb, and then run the following command:

Dir[Dir.pwd + "/*.rb"].each { |file| require file }

Which loads all the files into irb. Which brings me to my question: when I require a file, how does irb process that request? Does it take all requires and put them in one overall 'file' ? I am looking for the mechanics of how irb works.

If anyone has the answer or can point me in the right direction, I would appreciate it.

Cheers


Solution

  • The short answer is:

    require loads a file into the Ruby interpreter. The source code is analyzed, its by-products incorporated into the Ruby runtime (classes loaded, etc.), and then the source code is not saved anywhere and is eventually garbage collected (the memory it occupied is freed).