I have to Ruby files: one contains a module with some methods for statistical calculation, in the other file I want to call one of the methods in the module.
How can I do that in Ruby?
Is that the right way?
require 'name of the file with the module'
a=[1,2,3,4]
a.method1
Best Answer
Require needs the absolute path to the file unless the file is located in one of Ruby's load paths. You can view the default load paths with
puts $:
. It is common to do one of the following to load a file:Add the main file's directory to the load path and then use relative paths with require:
Ruby 1.8 code that only loads a single file will often contain a one-liner like:
Ruby 1.9 added require_relative:
In the module you will need to define the methods as class methods, or use Module#module_function: