On Wed, 2006-02-22 at 23:08 +0900, Ronald F. wrote:
variable abc is set to 4 and def is set to 5.
I tried the following:
But after this, abc is still undefined. What am I doing wrong here?
It’s just the way Ruby works with respect to deciding whether ‘a’ is a
variable or a method. Your assignment didn’t get ‘seen’ except inside an
eval. You can use eval again to get back there (changed the var names,
def is a keyword):
# => 4
# => 5
But that’s a hack, and really I think you should consider an alternate
design - local variables are supposed to be, well, local. Maybe
constants or (though it pains me to say it) globals would be more
Another technique I’ve used with great success is having a class that
acts as context for an external script, providing methods and holding
data in instance variables. You supply a script similar to yours above,
except using instance variables, which is then instance_eval’d in an
instance of that class, after which you have a self-contained
configuration, or whatever else it is you made.