abhilash
abhilash

Reputation: 41

How I include a module (this module has a module inside that) inside another module in Ruby

I have a module as following,

main.rb:

module Main
 include Dad::Mam
end

and

in dad.rb:

module Dad
 module Mam
  puts "Mam is saying you are very lazy..."
 end
end

How can I name this file? dad.rb is right?

but when running

$ ruby main.rb

I am getting an Error like,

main.rb:2:in <module:Main>': uninitialized constant Main::Dad (NameError) from main.rb:1:in'

I need to show the sentance inside the puts under Mam module while running ruby main.rb,

I am confused about using ruby's modules, please anyone help me and guide me..

Upvotes: 0

Views: 72

Answers (1)

Joshua Cheek
Joshua Cheek

Reputation: 31726

In this case, since you're just writing a simple script, use #require_relative

require_relative 'dad'
module Main
 include Dad::Mam
end

For an actual app or library, you would want to manage the load path (a global variable holding an array that tells ruby where to look for files) and then use a normal require

Upvotes: 1

Related Questions