From: Brett Williams Date: 2008-02-13T01:27:36+09:00 Subject: Re: Ruby equivalent to source command Robert Klemme wrote: > 2008/2/12, Brett Williams : >> 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' > [snip] > Or are you seeing this: > > 17:05:25 ~ > $ echo "x=123" | ruby -e 'eval(ARGF.read, binding); puts x' > -e:1: undefined local variable or method `x' for main:Object (NameError) > 17:06:31 ~ > $ > >> unless I extend the scope of x by setting it before sourcing the file. > > Like > > 17:06:31 ~ > $ echo "x=123" | ruby -e 'x=1; eval(ARGF.read, binding); puts x' > 123 > 17:07:32 ~ > $ Precisely. > This is because local variables are detected at compile time. That's > why Ruby thinks "x" in my bit above is a method because there is no > assignment. Aye, that's the problem. >> 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. > > A global is probably much more appropriate here. If the code you > source somehow generates configuration settings then maybe you can set > a global Hash that will receive values. It depends on the larger > context of what you want to achieve. I figured this was the case. I'll go with globals for my context. >> P.S.: Things have changed since I was a pretty regular poster back in >> 2002-2003... ;) > > Does this have to do with your email address? ;-) If yes, congrats > and greetings to Becky! Indeed no. Becky predates even my Ruby (which I started using as my primary language in 2001). I'm just very happy to see this list mirrored on ruby-forum which makes it much more convenient for me personally. The email address is one not used for much -- it functions mainly as a spam magnet =) -- Posted via http://www.ruby-forum.com/.