From: "Rishel,Wes" Date: 2007-07-02T01:52:20+09:00 Subject: Re: Newbie Question. eval.rb variable scope issue Thanks Chris, for taking the time to further this discussion. As you point out the community does seem to be very responsive. I regret that I got confused and did not recognize that the standard documentation is up-to-date. I got to several documents through the documentation sub-menu for Ruby Getting Started Link that were described as out of date for free and then spoke too hastily. To your main comment: The method eval is not what is featured in the tutorial, it is eval.rb, a script in the Ruby Samples library. My comments were directed to the use of the script in the tutorial. Through the Getting Started link that came with the Ruby install package, I ended up in a tutorial on this page: http://www.ruby-doc.org/docs/UsersGuide/rg/examples.html It is the page that recommends the use of eval.rb and, in fact, uses the script in a sequence of examples that extends across several web pages. On the next page, http://www.ruby-doc.org/docs/UsersGuide/rg/strings.html, there is an example: ruby> var = " abc " " abc " ruby> "1234#{var}5678" "1234 abc 5678" On my machine eval.rb gives a different result. ruby> var = " abc " " abc " ruby> "1234#{var}5678" ERR: undefined local variable or method `var' for main:Object ruby> Looking at the scoping rules for Ruby it is hard to understand how eval.rb was ever able to perform the examples in the tutorial, since the current output is entirely consistent with the scoping rule. It is also hard to perceive how one could write a version of eval.rb that would work, given that it pretty much has to have a loop. While I feel it would be premature for me to opine on the Ruby semantics, I have to say I am having difficulty perceiving the value of making context bindings on substructures of code within a method when there is no explicit declaration of the context block other than the loop-defining statement. It is hard to say which of this various principles this supports. Arguably, it is not the principle of least surprise, since deleting a statement in the start of a method can alter the method's behavior many lines down in a loop. The rationales I can think of would be about helping the computer run more efficiently. It seems that the philosophies around Ruby are oriented to helping the programmer rather than helping the machine. So I guess I am missing the manner in which this helps. I am not holding Python up for comparison, just trying to understand Ruby on its own terms. -----Original Message----- From: Chris Carter [mailto:cdcarter@gmail.com] Sent: Sunday, July 01, 2007 8:40 AM To: ruby-talk ML Subject: Re: Newbie Question. eval.rb variable scope issue On 7/1/07, Rishel,Wes wrote: > Thanks, Bill. It sounds like irb will fill my needs. > > I have to say that featuring eval in the tutorials when it doesn't work > for a simple set of interactions like setting a variable and then > printing it seems very newbie unfriendly. > > Some languages have a culture that excludes all but the smartest and > those who are willing to devote all their time to memorizing the > nuances. I would put Perl in that category. The initial "affect" of the > Ruby language and community is else-wise, but a couple of things make me > wonder. This includes tutorials that don't work in every case and the > fact that there is no free version of documentation for the current > version, which was released > 3 months ago. > > If it weren't for people like you willing to respond it could be very > off-putting indeed. > I would like to know which tutorial out there you were following that teaches use of eval(). The Ruby Way generally does not recommend the use of eval with a string unless you are doing some very advanced work, and even then it is very risky business. Ruby does have it's nuances, but this community, as you can see, is quite willing to help you out when you have found one, and help you figure out what it is and why it is. I don't know which tutorial you are referring to, because none of the "official" tutorials on the ruby-lang.org website mention eval. If you want a good place to start learning ruby, I suggest you check out _why's poignant guide to ruby (http://poignantguide.net/ruby/), it is a little wacky, but it teaches the language and the concepts of the ruby way very well. As to no free documentation for the most recent version of ruby, one ships with your ruby install (the ri tool and the rdocs) and there is ruby-doc.org. Welcome to Ruby, if you hit any other major problems feel free to ask. We also have a lively IRC channel #ruby-lang on freenode. Hope you like the language. And be aware, the philosophy guiding Ruby is not the same as that of python's. -- Chris Carter concentrationstudios.com brynmawrcs.com This e-mail message, including any attachments, is for the sole use of the person to whom it has been sent, and may contain information that is confidential or legally protected. If you are not the intended recipient or have received this message in error, you are not authorized to copy, distribute, or otherwise use this message or its attachments. Please notify the sender immediately by return e-mail and permanently delete this message and any attachments. Gartner makes no warranty that this e-mail is error or virus free.