Problem with variables

Hi.

I’ve a problem with variables.
I define a global variable outside a method and then I use this variable
in a method. And thats my problem. I can print the value of the variable
but in the line with the eval() the place holder (#{temp}) is not
replaced and the output is empty. But why.

Can anybody help me?

#!/usr/bin/ruby

$temp = “Hallo”

def test()
puts $temp # -> Hallo … all is okay
test = “#{temp} World"
puts eval( '”’ + test + ‘"’ ) # -> … nothing … but why?
end

print "Content-type: text/html\n\n"
print test()

bye
Dirk Einecke

Hi –

···

On Mon, 16 Feb 2004, Dirk Einecke wrote:

Hi.

I’ve a problem with variables.
I define a global variable outside a method and then I use this variable
in a method. And thats my problem. I can print the value of the variable
but in the line with the eval() the place holder (#{temp}) is not
replaced and the output is empty. But why.

Can anybody help me?

#!/usr/bin/ruby

$temp = “Hallo”

def test()
puts $temp # → Hallo … all is okay
test = “#{temp} World”

The variable name you want is $temp, not temp. (Assuming you really
want to use a global variable, which is another matter… :slight_smile:

David


David A. Black
dblack@wobblini.net

“Dirk Einecke” dirk.einecke@gmx.de wrote in message
news:c0or2a$19s6c2$1@ID-43561.news.uni-berlin.de

puts $temp # → Hallo … all is okay
test = “#{temp} World”
puts eval( ‘"’ + test + ‘"’ ) # → … nothing … but why?

test = “#{$temp} World”

Dirk Einecke wrote:

Hi.

I’ve a problem with variables.
I define a global variable outside a method and then I use this variable
in a method. And thats my problem. I can print the value of the variable
but in the line with the eval() the place holder (#{temp}) is not
replaced and the output is empty. But why.

Can anybody help me?

#!/usr/bin/ruby

$temp = “Hallo”

def test()
puts $temp # → Hallo … all is okay
test = “#{temp} World”
You’re using a global var but here you’re asignining a local var that is
undefined(actually nil) and that’s why, change line to:
test = “#{$temp} World”
puts eval( ‘"’ + test + ‘"’ ) # → … nothing … but why?
end

print “Content-type: text/html\n\n”
print test()
just use test()

bye
Dirk Einecke

After that change you’ll get:

Content-type: text/html

Hallo
Hallo World

cheers
Adartse

test = “#{temp} World”

should be:

test = “#{$temp} World”

JA

Hi.

“Dirk Einecke” dirk.einecke@gmx.de wrote in message
news:c0or2a$19s6c2$1@ID-43561.news.uni-berlin.de

puts $temp # → Hallo … all is okay
test = “#{temp} World”
puts eval( ‘"’ + test + ‘"’ ) # → … nothing … but why?

test = “#{$temp} World”

Thanks to all (Sean Ross, Osuka Adartse, John Andrews).
I works. :slight_smile:

bye
Dirk Einecke