I have a Ruby code file (somelogic.rb) that contains several methods and classes, located in say, /home/user/code. Now I'm writing another class in the same directory and would like to reference to the methods and classes in somelogic.rb. How do I do that? I greatly appreciate any input.
If you are using Ruby 1.9 or later, this is the simplest way to do it:
require_relative 'somelogic'
If you want your code to work in 1.9 and older versions of Ruby, you should do this instead:
require File.join File.dirname(__FILE__), 'somelogic'
Whichever line you choose, you should put it at the top of your ruby file. Then any classes, modules, or global variables defined in somelogic.rb will be available to your program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With