3

Is there a way how to require all files in lib directory at once in irb console?:

irb ( project root )
require './lib/' # not working  

structure

.
|
--lib 
  |
  |-- one.rb
  |-- two.rb
  |-- tree.rb

EDIT

I prefer a solution where I can require files only once, not each time when I start irb session.

  • Please do `require_relative './lib/one.rb'`, `require_relative './lib/two.rb'` and ``require_relative './lib/three.rb'`` – Arup Rakshit Mar 07 '14 at 12:57
  • read this - http://stackoverflow.com/questions/3672586/what-is-the-difference-between-require-relative-and-require-in-ruby – Arup Rakshit Mar 07 '14 at 13:03
  • @ArupRakshit let say I am working with 5 files...it is too much typing ...prefer a solution where I can require them once. –  Mar 08 '14 at 18:20
  • Yes... see the [answer](http://stackoverflow.com/a/22250846/2767755).. – Arup Rakshit Mar 08 '14 at 18:21
  • @ArupRakshit when you exit irb session and come back, you have to do it again...prefer sawa's solution - see my comment in his answer pls. –  Mar 08 '14 at 18:25
  • Ok... Choose what meets your need. – Arup Rakshit Mar 08 '14 at 18:27

3 Answers3

2

As described in the documentation require (and require_relative) can't take the name of a directory as argument, just a single file name. You could write something like the following to do what you want:

Dir['./lib/*.rb'].each { |f| require_relative(f) }
toro2k
  • 19,020
  • 7
  • 64
  • 71
  • 1
    I don't know why this got a downvote, it answers the question exactly. – Gareth Mar 07 '14 at 13:14
  • 1
    I am not a downvoter, but I don't think it is practical to type this each time in irb. Also, the `f` is already an absolute path, so `require` will suffice, unless you have thoughts that `require_relative` should be constantly used taking over `require` (which I don't argue against). – sawa Mar 07 '14 at 14:27
  • 1
    @sawa Obviously if OP has to require the files any time he runs irb, my answer is not an option. Regarding the use of `require_relative`: I use it to require files from dirs I don't have listed in `$:`. You should argue against constantly replacing `require_relative` for `require` because it just doesn't work, for example using `require_relative` you can't load files from gems. – toro2k Mar 07 '14 at 15:15
2

Create a file named .irbrc in your home directory, and write require commands for whatever file you want to require in there. When you run irb, .irbrc will be loaded.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • Isn't the question about *which* commands need to be used to require everything in a folder? – Gareth Mar 07 '14 at 14:11
  • I don't think so. I think the OP wants a way to easily require several files (that happen to be in a single directory) at irb start up (be it automatic or manual). I am suggesting an automatic way. – sawa Mar 07 '14 at 14:16
  • The broken code they are using shows `require` called with a folder (which doesn't work). They do mention `irb` but not startup. – Gareth Mar 07 '14 at 14:18
  • It is the first line after the `irb` command. And, even if the OP weren't asking it to be done at start up, what difference would it make? You think that the timing of require matters, and the OP needs to do something in irb before loading the files? – sawa Mar 07 '14 at 14:19
  • I figured that was just because this was an example. What should they have put instead? Anyway, I guess we'll have to wait until an answer is selected :) – Gareth Mar 07 '14 at 14:22
  • "You think that the OP needs to do something in irb before loading the files" - No, but just because I require certain files when I'm working on one project doesn't mean I need them every time I go into `irb` – Gareth Mar 07 '14 at 14:23
  • @sawa created `.irbrc` in home dir and wrote there require './lib/one.rb`, tried also w/o rb...but it does not work...What I am missing there ? Btw I like this solution and will accept your answer when it will be working. –  Mar 08 '14 at 16:13
  • @etmo999 From your comment, I cannot tell in which way it is not working. Try `puts "foo"` in the first line of `.irbrc` and see if it runs. If it does not, then something is wrong with your IRBRC path. If it does show but the file `require`-d does not work, then your path `./lib/one.rb` is wrong. – sawa Mar 08 '14 at 21:27
  • @etmo999 I saw your directory structure again. Your path is wrong. Do `Dir["./lib/*"].each{|f| require(f)}`. – sawa Mar 08 '14 at 21:29
  • So in the end the problem *was* about how the files needed to be required. Exactly like the other answer – Gareth Mar 11 '14 at 12:38
  • @Gareth If you want to type the command `Dir["./lib/*"].each{|f| require(f)}` each time in irb, then accept another answer. The point of my answer, as well as how I interpreted what you wanted to do, was to avoid typing that in irb every time. – sawa Mar 11 '14 at 13:08
0

You can use this

Dir[File.dirname(__FILE__) + '/lib/*.rb'].each {|file| require file }
sidney
  • 2,704
  • 3
  • 28
  • 44