Search code examples
rubylibgosu

Ruby Gosu using multiple folders


i am working on a Ruby Gosu project and i was wondering if there was a way to access different code from multiple folders and connect them too a main base code,i believe this would make the overall project much less messy and i would not have to have one unorganized 500 line project. basically i want a command of sorts that will access and run code from other folders.


Solution

  • Move the files to one specific folder. You can do this manually or you can do it through the terminal or cmd, for example:

    $mkdir project;$mv myfile.rb ~/mydir/project <= Linux commands, OSX are the same

    $mkdir project;$move myfile.rb /home/project <= Windows commands

    From there do require_relative 'myfile.rb' this will require the file for the program to run, example:

    require_relative 'myfile.rb'
    
    def some_name
      puts "something
    end
    

    If you don't want to do all that moving you can do require and give it the argument with the full path to the file, example:

    require '/home/homedir/myfile.rb' #<= you can also use require_relative
    
    def some_name
      puts "something"
    end
    

    Depending on how you want the file to be used and how often you can use load 'filename' the more Ruby way is to use require 'filename'or require_relative 'filename', however.

    load uses the file every time the method is executed, while require only loads the file once.

    I'd highly suggest you read up on how to work with files and how to require files within a program: http://rubylearning.com/satishtalim/including_other_files_in_ruby.html