0

I write the gem, it's the addition to faker gem (with russian functions like tax & vat et.c.)

So, I have a trouble: every piece of code is big, so I need to split it to logical parts.

IE I have Inn functionality to be called as Faker::Russian.inn()

So, I write

Dir['faker/russian/*.rb'].each { |file| require file }

module Faker
  class Russian
    extend Inn
  end
end

Else I have (at faker/russian/inn.rb) (which is required) this file

module Inn
  def inn ; puts 'inn goes here' ; end
end

But I have an error: lib/faker/russian.rb:5:in <class:Russian>': uninitialized constant Faker::Russian::Inn (NameError)

How can I avoid this error and successfully include code and extend all connected modules automatically?

Alex Antonov
  • 14,134
  • 7
  • 65
  • 142

1 Answers1

3

Option 1 is to refer to top level namespace when extend

extend ::Inn

Option 2 is to define Inn with correct namespace

module Faker
  module Russian
    module Inn
      def inn; end
    end
  end
end

In a gem option 2 is preferred to have all modules namespaced under this gem's top module. Imagine if you use option 1 and have an Inn module in app but not gem, you'll meet problem.

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • I use the 2nd way and still get this: `': uninitialized constant Faker::Russian::Inn (NameError)` – Alex Antonov Jun 24 '14 at 02:20
  • 1st way don't work too. I get `': uninitialized constant Inn (NameError)`. Maybe I don't require files well? – Alex Antonov Jun 24 '14 at 02:23
  • I see. There is a name conflict in #2. The module is named "Russian", while the class name is also "Russian". Give them suitable names and they should be fine. – Billy Chan Jun 24 '14 at 02:39