From: Mason Kelsey Date: 2009-08-25T10:47:34+09:00 Subject: Re: Ruby eval --001485f7252e85764e0471ed84bd Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Others can correct me if I'm wrong, as I'm also new to Ruby. (My 127th language.) But eval , which also exist in several other languages, is used when you need to change a COMMAND dynamically, after the program starts running and is beyond the reach of the programmer. Since that is a fairly complex situation and beyond the need for most programmers, eval should normally not be used in production systems. I suspect that eval would be caught in any production walkthrough by other professional programmers as a potential security threat, since it would be easy to obscure what the code is actually doing. For example, it could be used to create a trap door. In that case the programmer would have to rewrite the code without using eval to comply. So it is a waste of valuable time in most production systems development or maintenance to use that command. I assume that eval in Ruby is the same thing. Use eval for experimental or academic stuff, for example in artificial intelligence. No Sam On Mon, Aug 24, 2009 at 3:24 PM, Brandon Larocque wrote: > I am trying to learn Ruby as a first language, ( I tended to go back and > forth over languages in my studies, but I plan, now, to focus on Ruby ), > and came across a confusing problem. My brother likes to play Mafia Wars > on facebook and asked me to come up with something simple. So I put a > little something together, and it involved the eval function. > > From what I've read, recently, people tend to look down on the eval > function? Why is this? I have a few classes that have multiple names, so > I decided to use it to work through the problem I was having ( I didn't > want to write a bunch of loops ). For example : > > ( $jobarea == "hi" or $jobarea == "es" ) ? (loop = 9) : ( ($jobarea == > "ca") ? (loop = 8) : (loop = 7) ) > expcost = [] ; exppay = [] ; pay = [] ; name = [] > for i in (1..loop) do > eval("expcost.push $#{$jobarea}_job#{i}.expcost") > eval("exppay.push $#{$jobarea}_job#{i}.exppay") > eval("pay.push $#{$jobarea}_job#{i}.pay") > eval("name.push $#{$jobarea}_job#{i}.name") > end > > Is there any way that I might be able to do this without eval? Or is it > alright the way I have done it? > > Regards, > Brandon LaRocque > -- > Posted via http://www.ruby-forum.com/. > > --001485f7252e85764e0471ed84bd--