Defining Functions in Objects and Variable Scoping
When a function is defined within an object, like variables in the object, are part of that object' scope. It seems that inside the function, the same rules apply. Variables I define (by writing to) in the function are part of the object, unless I make them local. Prior to be written to, variables in the global scope can be accessed; but when I try to change them, new object variables are created. This is reasonable behavior, I can use "global." to force a global write.
I am doing this inside of a class. I have an object holding functions, and I reference them with string. It's an alternative to a sequence of: if...elsif...end. I can't figure out a way for these functions in an object to directly access my class variable. I am instead, passing the class instance to the function as a parameter, and accessing the class variables.
I am also doing this with a list of functions, it's pretty handy. So, I was wondering, do variables created in a function inside a list becomes part of the list? How can I access them? The answer is yes, because a list is just an object which can be extended.
mylist[index]() // for the function inside
mylist.myvar // for the variable created inside function
