Forum: Ruby cross require files

F75c135553653787f6025709a4e09ed6?d=identicon&s=25 Rajarshi Chakravarty (raj_plays)
on 2014-02-18 08:15
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
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-18 08:50
Also provide the us the content of `A.rb` and `B.rb`. we need to look
into the same.
F75c135553653787f6025709a4e09ed6?d=identicon&s=25 Rajarshi Chakravarty (raj_plays)
on 2014-02-18 09:26
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.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-18 09:30
Rajarshi Chakravarty 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 ?
F75c135553653787f6025709a4e09ed6?d=identicon&s=25 Rajarshi Chakravarty (raj_plays)
on 2014-02-18 09:49
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?
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-18 10:48
Rajarshi Chakravarty 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.
F75c135553653787f6025709a4e09ed6?d=identicon&s=25 Rajarshi Chakravarty (raj_plays)
on 2014-02-18 13:11
Thank you for your help, Arup.

I was able to solve my problem using $ vars and if __FILE__ == $0 in the
Processor class.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-18 13:55
Rajarshi Chakravarty 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` ?
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2014-02-18 21:25
(Received via mailing list)
Am 18.02.2014 10:48, schrieb Arup Rakshit:
> Rajarshi Chakravarty 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.
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2014-02-18 21:31
(Received via mailing list)
Am 18.02.2014 13:11, schrieb Rajarshi Chakravarty:
> 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
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-18 21:39
unknown wrote in post #1137103:
> Am 18.02.2014 10:48, schrieb Arup Rakshit:

> 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 ?
F75c135553653787f6025709a4e09ed6?d=identicon&s=25 Rajarshi Chakravarty (raj_plays)
on 2014-02-20 14:57
I use ver. 1.8. In that 'require' can access all files in the current
dir.
F75c135553653787f6025709a4e09ed6?d=identicon&s=25 Rajarshi Chakravarty (raj_plays)
on 2014-02-20 15:00
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?
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.