user9450045
user9450045

Reputation:

How to distinguish classes with the same name defined in different Gems?

I have two gems pdf-reader and origami. Each gem has a module called PDF. When I call them in the following order,

# Importing first gem
requires 'pdf/reader'

# Second gem
requires origami
include Origami

PDF::Reader.new(dir) # (first gem)
PDF.read dir # (second gem) #>> undefined method 'read' for PDF:Module (NoMethodError)

the first gem has no problem, but when I get to the second one, I get a mistake alleging that pdf does not have the read method. But if I first import 'origami' and then 'pdf/reader', I get error:

PDF is not a module (TypeError)

Ruby gets confused on which gem I'm invoking.

How can I specify that I'm talking about the specific first or second gem?

Upvotes: 1

Views: 243

Answers (1)

Kimmo Lehto
Kimmo Lehto

Reputation: 6041

In your specific example, the include Origami is unnecessary, I don't know why they added that example to their README.

require 'pdf/reader'
require 'origami'

PDF::Reader.new(dir) # (first gem)
Origami::PDF.read dir # (second gem)

Sometimes it may be possible to do something like:

require 'pdf/reader'

PDFReader = PDF::Reader

require 'origami'
include Origami

PDFReader.new(dir)
PDF.read(dir)

Upvotes: 1

Related Questions