# Global variable in ruby

**URL:** <https://www.ruby-forum.com/t/global-variable-in-ruby/186633>\
**Category:** Ruby\
**Created:** [April 10, 2010, 3:21am UTC](https://www.ruby-forum.com/t/global-variable-in-ruby/186633 "2010-04-10T03:21:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 10, 2010, 3:21am UTC](https://www.ruby-forum.com/t/global-variable-in-ruby/186633/1 "2010-04-10T03:21:49Z")

</div>

Trying to use some global variable

$buffer = Array.new  
4  
5 def save\_log(data, file\_name)  
6 puts ‘save log called ‘+file\_name  
7 open(file\_name, ‘w’) { |f| f.write(data) }  
8 end  
9  
10 def get\_citations\_from\_page(data, start\_point, end\_point)  
11 citations = get\_substring\_within\_inclusive(data,  
start\_point, end\_point)  
12 u\_arr = getURLsFromPage(‘[http://www.xxxx.com/](http://www.xxxx.com/)’, citations)  
13 u\_arr.each {  
14 |t|  
15 if  
(!beginsWith(t[1],‘[XXXX](http://www.xxxx.com//gp/reader)’))  
16 buffer.push t[0]+’%%%’+t[1]  
17 end  
18 }  
19 end

getting error:

xxxx\_citations.rb:16:in `get_citations_from_page': undefined local variable or method `buffer’ for main:Object (NameError)  
from xxxx\_citations.rb:13:in `each' from xxxx_citations.rb:13:in `get\_citations\_from\_page’  
from xxxx\_citations.rb:26:in `retrieve_citations' from xxxx_citations.rb:46 from xxxx_citations.rb:42:in `open’  
from xxxx\_citations.rb:4

Why? How to solve it?

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 10, 2010, 3:34am UTC](https://www.ruby-forum.com/t/global-variable-in-ruby/186633/2 "2010-04-10T03:34:45Z")

</div>

On Apr 9, 2010, at 6:21 PM, Amishera Amishera wrote:

> 11 citations = get\_substring\_within\_inclusive(data,
> 
> Why? How to solve it?

Maybe because you meant $buffer ? 😉

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 10, 2010, 4:48am UTC](https://www.ruby-forum.com/t/global-variable-in-ruby/186633/3 "2010-04-10T04:48:44Z")

</div>

Amishera Amishera wrote:

> Trying to use some global variable
> 
> $buffer = Array.new

While global variables are really nice, most programmers try not to use  
them, as they cause a lot of errors in the long end, dealing with other  
programs or add-ons. I try not to use Global Variables in my programs,  
but when I do need to, I name them so specifically, that they can not be  
confused with another program’s global variables.

i.e. $program\_name\_buffer00 = Array.new

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 10, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/global-variable-in-ruby/186633/4 "2010-04-10T22:54:29Z")

</div>

Amishera Amishera wrote:

> Trying to use some global variable
> 
> $buffer = Array.new  
> 4  
> 5 def save\_log(data, file\_name)  
> 6 puts 'save log called '+file\_name  
> 7 open(file\_name, ‘w’) { |f| f.write(data) }  
> 8 end  
> 9  
> 10 def get\_citations\_from\_page(data, start\_point, end\_point)  
> 11 citations = get\_substring\_within\_inclusive(data,  
> start\_point, end\_point)  
> 12 u\_arr = getURLsFromPage(‘[http://www.xxxx.com/](http://www.xxxx.com/)’, citations)  
> 13 u\_arr.each {  
> 14 |t|  
> 15 if
> 
> > (!beginsWith(t[1],‘[XXXX](http://www.xxxx.com//gp/reader)’))  
> > 16 buffer.push t[0]+‘%%%’+t[1]  
> > 17 end  
> > 18 }  
> > 19 end

The reason this is happening is because you forgot to write $ in  
buffer.push.

It should be $buffer.push.

But you could just put a buffer var to get passed into the function.

> 10 def get\_citations\_from\_page(data, start\_point, end\_point, buffer)

and then just write buffer for the last argument.

---

<div class="post-metadata">

**Author:** ![amishera](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@amishera](https://www.ruby-forum.com/u/amishera)\
**Post date:** [April 10, 2010, 8:06pm UTC](https://www.ruby-forum.com/t/global-variable-in-ruby/186633/5 "2010-04-10T20:06:03Z")

</div>

Jesse J. wrote:

> > $buffer = Array.new
> 
> While global variables are really nice, most programmers try not to use  
> them, as they cause a lot of errors in the long end, dealing with other  
> programs or add-ons. I try not to use Global Variables in my programs,  
> but when I do need to, I name them so specifically, that they can not be  
> confused with another program’s global variables.
> 
> i.e. $program\_name\_buffer00 = Array.new

If it’s just that a number of methods want access to the same buffer,  
you probably want to put them in a class and use an instance variable.

class PageParser  
attr\_reader :buffer

def initialize(buffer = [])  
@buffer = buffer  
end

def get\_citations\_from\_page(data, start\_point, end\_point)  
citations = get\_substring\_within\_inclusive(data, start\_point,  
end\_point)  
u\_arr = getURLsFromPage(‘[http://www.xxxx.com/](http://www.xxxx.com/)’, citations)  
u\_arr.each { |t|  
if (!beginsWith(t[1],‘[XXXX](http://www.xxxx.com//gp/reader)’))  
@buffer.push t[0]+‘%%%’+t[1]  
end  
}  
end

def get\_substring\_within\_inclusive  
…  
end  
end

parser = PageParser.new  
parser.get\_citations\_from\_page(…)  
p parser.buffer

If you really want a single buffer which is global to the whole program,  
then I’d say an instance variable of a Class is the way to go, which  
avoids the global variable namespace problem.

class PageParser  
@buffer = []  
def self.buffer  
@buffer  
end  
end

PageParser.buffer \<\< “hello”  
PageParser.buffer \<\< “world”  
p PageParser.buffer

In the above code, @buffer is an instance variable of the Class object,  
not of instances of class PageParser (since class PageParser is itself  
an object)
