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

···

--
Posted via http://www.ruby-forum.com/.

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

_________file2.rb_________
module MyModule
  def func_b
    ...
  end
end

Jesus.

···

On Mon, Nov 7, 2011 at 5:29 PM, Andreas L. <andreas.lundgren@takepoint.se> wrote:

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)")

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

···

-----Original Message-----
From: Jesús Gabriel y Galán [mailto:jgabrielygalan@gmail.com]
Sent: Monday, November 07, 2011 8:33 AM
To: ruby-talk ML
Subject: Re: How to include a function from a different file in a module

On Mon, Nov 7, 2011 at 5:29 PM, Andreas L. <andreas.lundgren@takepoint.se> wrote:

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)")

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

_________file2.rb_________
module MyModule
  def func_b
    ...
  end
end

Jesus.