B
Brett Williams
Hello all,
I am trying to make a "source" command for Ruby (a la sh or tcl), but I
think lexical scoping is tripping me up. I've done a lot of reading
about bindings, and it seems I just can't do what I'm trying to do.
def source(filename, bind = TOPLEVEL_BINDING)
code = nil
File.open(filename) { |f| code = f.read }
eval(code, bind)
end
This works as I would like, with the exception of local variables
present in the sourced file. For example, if I had something like this
in the sourced file:
x = "value set in sourced file"
then I get an error : undefined local variable or method 'x'
unless I extend the scope of x by setting it before sourcing the file.
Is there any way to accomplish what I want? I could work around this by
sticking to instance variables or *gulp* global variables for this
application, but before I throw in the towel I wanted to throw this out
to people here.
Thanks,
Brett Williams
P.S.: Things have changed since I was a pretty regular poster back in
2002-2003...
I am trying to make a "source" command for Ruby (a la sh or tcl), but I
think lexical scoping is tripping me up. I've done a lot of reading
about bindings, and it seems I just can't do what I'm trying to do.
def source(filename, bind = TOPLEVEL_BINDING)
code = nil
File.open(filename) { |f| code = f.read }
eval(code, bind)
end
This works as I would like, with the exception of local variables
present in the sourced file. For example, if I had something like this
in the sourced file:
x = "value set in sourced file"
then I get an error : undefined local variable or method 'x'
unless I extend the scope of x by setting it before sourcing the file.
Is there any way to accomplish what I want? I could work around this by
sticking to instance variables or *gulp* global variables for this
application, but before I throw in the towel I wanted to throw this out
to people here.
Thanks,
Brett Williams
P.S.: Things have changed since I was a pretty regular poster back in
2002-2003...