Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a method in another Ruby code file?

Tags:

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.

like image 718
Bryan Avatar asked Nov 14 '11 00:11

Bryan


1 Answers

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.

like image 181
David Grayson Avatar answered Sep 17 '22 15:09

David Grayson