global variable in ruby

A

Amishera Amishera

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

Gennady Bystritsky

Trying to use some global variable
=20
$buffer =3D 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 =3D get_substring_within_inclusive(data,
start_point, end_point)
12 u_arr =3D 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
=20
getting error:
=20
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
=20
Why? How to solve it?

Maybe because you meant $buffer ? ;-)
 
J

Jesse Jurman

Amishera said:
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
 
B

Brian Candler

Jesse said:
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)
 
H

H- 16

Amishera said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top