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
“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
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.
bye
Dirk Einecke