Search code examples
rubyload-path

Loading a Ruby file in Windows 7. How do I do this? Loadpath issue?


I can't seem to load files. I am using Ruby 2.0.0 x64 and the built in command prompt with Ruby.

I have 2 problems. 1) If I use Powershell or the cmd.exe, I can't access Ruby if I type in irb. Any idea how to connect the two.

2) So instead I use the downloaded command prompt with Ruby. I have a file created called banking.rb. I am trying to load the file so I can test my code. Here is the location of my file:

C:\Users\Jwan\Desktop\Ruby Programs.

When I type in load 'banking.rb', I get this error:

LoadError:  cannot load such file -- banking.rb  
    from <irb>:6:in 'load'
    from <irb>:6
    from C:/Ruby200-x64/bin/irb:12:in '<main>'

So my guess is that the ruby loadpath is incorrect? The folder where this file is located is on my desktop. How do I change my loadpath (full disclosure:, please try to dumb down the instructions. I didn't even know what loadpath was prior to this post)


Solution

  • Check the Ruby docs for Kernel.load:

    Loads and executes the Ruby program in the file filename. If the filename does not resolve to an absolute path, the file is searched for in the library directories listed in $:.

    So you can either type the absolute path

    load 'C:\Users\Jwan\Desktop\Ruby Programs\banking.rb'
    

    or modify $:

    $: << 'C:\Users\Jwan\Desktop\Ruby Programs'
    load 'banking.rb'
    

    $: can also be referenced with $LOAD_PATH. It is the array of directories that Ruby searches for files to load. If you want to permanently add directories to your load path, you can set the RUBYLIB environment variable to a colon-separated list of them (look up how to do this on Windows, it's nestled deep in some menu).