# Cross require files

**URL:** <https://rubytalk.org/t/cross-require-files/69176>\
**Category:** ruby-talk\
**Created:** [18 February 2014 07:15 UTC](https://rubytalk.org/t/cross-require-files/69176 "2014-02-18T07:15:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 February 2014 07:15 UTC](https://rubytalk.org/t/cross-require-files/69176/1 "2014-02-18T07:15:54Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 February 2014 07:50 UTC](https://rubytalk.org/t/cross-require-files/69176/2 "2014-02-18T07:50:45Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [20 February 2014 13:57 UTC](https://rubytalk.org/t/cross-require-files/69176/3 "2014-02-20T13:57:38Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [20 February 2014 14:00 UTC](https://rubytalk.org/t/cross-require-files/69176/4 "2014-02-20T14:00:46Z")

</div>

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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 February 2014 08:26 UTC](https://rubytalk.org/t/cross-require-files/69176/5 "2014-02-18T08:26:17Z")

</div>

Processor.rb

> **···**
>
> -------
> 
> require 'Parser'
> 
> Parser.parse("Exhibitors.csv", ",")
> 
> def self.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.
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 February 2014 08:30 UTC](https://rubytalk.org/t/cross-require-files/69176/6 "2014-02-18T08:30:27Z")

</div>

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 ?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 February 2014 08:49 UTC](https://rubytalk.org/t/cross-require-files/69176/7 "2014-02-18T08:49:56Z")

</div>

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.  
How to do that?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 February 2014 09:48 UTC](https://rubytalk.org/t/cross-require-files/69176/8 "2014-02-18T09:48:45Z")

</div>

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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 February 2014 12:11 UTC](https://rubytalk.org/t/cross-require-files/69176/9 "2014-02-18T12:11:11Z")

</div>

Thank you for your help, Arup.

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [18 February 2014 20:24 UTC](https://rubytalk.org/t/cross-require-files/69176/10 "2014-02-18T20:24:46Z")

</div>

@Arup

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

> **···**
>
> 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.
> 
> --  
> GitHub: [stomar (Marcus Stollsteimer) · GitHub](https://github.com/stomar/)  
> PGP: 0x6B3A101A

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 February 2014 12:55 UTC](https://rubytalk.org/t/cross-require-files/69176/11 "2014-02-18T12:55:50Z")

</div>

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` ?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [18 February 2014 20:30 UTC](https://rubytalk.org/t/cross-require-files/69176/12 "2014-02-18T20:30:26Z")

</div>

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

> **···**
>
> 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.
> 
> --  
> GitHub: [https://github.com/stomar/](https://github.com/stomar/)  
> PGP: 0x6B3A101A

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [18 February 2014 20:39 UTC](https://rubytalk.org/t/cross-require-files/69176/13 "2014-02-18T20:39:05Z")

</div>

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 ?
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
