Global variable in ruby

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/’, citations)
13 u_arr.each {
14 |t|
15 if
(!beginsWith(t[1],‘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 methodbuffer’ for main:Object (NameError)
from xxxx_citations.rb:13:in each' from xxxx_citations.rb:13:inget_citations_from_page’
from xxxx_citations.rb:26:in retrieve_citations' from xxxx_citations.rb:46 from xxxx_citations.rb:42:inopen’
from xxxx_citations.rb:4

Why? How to solve it?

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 ? :wink:

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

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/’, citations)
13 u_arr.each {
14 |t|
15 if

(!beginsWith(t[1],‘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.

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/’, citations)
u_arr.each { |t|
if (!beginsWith(t[1],‘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)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs