How to include a function from a different file in a module

Hi!

Would it be possible to have a module with one or several of its methods
(source code) stored in a different file?

Something like this with “func_b” stored in a different file than the
module.

file1.rb
module MyModule
def func_a

end

require “file2.rb”

end

file2.rb
def func_b

end

(This gived the error “private method `func_b called for MyModule:Module
(NoMethodError)”)

BR,
Andreas

On Mon, Nov 7, 2011 at 5:29 PM, Andreas L.
[email protected] wrote:

def func_a
end

(This gived the error “private method `func_b called for MyModule:Module
(NoMethodError)”)

In file2.rb you have to place func_b inside the module too:

file2.rb
module MyModule
def func_b

end
end

Jesus.

Classes are open. So are modules. You can define your module in 2 files
as Jesus described.

To use the module, you need to require both files.

require ‘file1’
require ‘file2’

Hope this helps.

Best,
Jingjing

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs