Cross require files

Hi,
I have 2 source files…A.rb and B.rb
Methods in A access methods in B and vice versa.
So i put require ‘B’ in A.rb and require ‘A’ in B.rb but it gives
NameError.
Please help.

Ver. 1.8

Also provide the us the content of A.rb and B.rb. we need to look
into the same.

Processor.rb

require ‘Parser’

Parser.parse(“Exhibitors.csv”, “,”)

def processRecord(tile_name, category_name, block_name_arr)

end

Parser.rb

require ‘Processor’

class Parser

def self.parse(dataFile, delimiter)


processRecord(tile, category, [block1, block2, block3, block4])

end

When i run Processor, it gives a NameError on
Parser.parse(“Exhibitors.csv”, “,”)

Please help.

Yes, they are in the same directory.
I think the problem is Processor.rb has some statements that get
executed when it is ‘required’ in the other file.
I want just the method to be accessible in the other file.
How to do that?

Rajarshi C. wrote in post #1137007:

When i run Processor, it gives a NameError on
Parser.parse(“Exhibitors.csv”, “,”)

Please help.

Nice! Can you show us the Filesystem structure of those 2 files? Are
they in the same directory ?

Rajarshi C. wrote in post #1137010:

Yes, they are in the same directory.
I think the problem is Processor.rb has some statements that get
executed when it is ‘required’ in the other file.
I want just the method to be accessible in the other file.
How to do that?

You need to use require_relative, instead of require. Please try
this.

Thank you for your help, Arup.

I was able to solve my problem using $ vars and if FILE == $0 in the
Processor class.

Rajarshi C. wrote in post #1137036:

Thank you for your help, Arup.

I was able to solve my problem using $ vars and if FILE == $0 in the
Processor class.

Nice to hear! But did you try require_relative ?

Am 18.02.2014 13:11, schrieb Rajarshi C.:

Thank you for your help, Arup.

I was able to solve my problem using $ vars and if FILE == $0 in the
Processor class.

That sounds so wrong. Global variables usually are considere harmful
or at least really bad style.

You really should try to restructure your code and avoid
circular dependencies if at all possible.

Regards,
Marcus

unknown wrote in post #1137103:

Am 18.02.2014 10:48, schrieb Arup R.:

You are completely on the wrong track: in Ruby 1.8 there is
no require_relative! Using require is correct.

But as far as I know, to work require 'Processor', the CWD should be
added to the $LOAD_PATH. Am I right ?

Am 18.02.2014 10:48, schrieb Arup R.:

Rajarshi C. wrote in post #1137010:

Yes, they are in the same directory.
I think the problem is Processor.rb has some statements that get
executed when it is ‘required’ in the other file.
I want just the method to be accessible in the other file.
How to do that?

You need to use require_relative, instead of require. Please try
this.

@Arup

You are completely on the wrong track: in Ruby 1.8 there is
no require_relative! Using require is correct.

I use ver. 1.8. In that ‘require’ can access all files in the current
dir.

Hi Marcus,
My main.rb file has some variables that are initialized once and then
they are accessed inside a method.
How should i scope such vars?