In article <20010504123943.C2523 / scheibenwelt>,
Marko Schulz  <in6x059 / public.uni-hamburg.de> wrote:
>On Fri, May 04, 2001 at 08:11:24AM +0900, Phil Tomson wrote:
>> 
>>          srchStr = "/Error/"  
>  :
>>            if eval srchStr
>
>This construct will not work as expected, it will always go into the
>'true'-branch. The eval will result in an object of class Regexp. And
>since all objects beside nil and false are true in ruby, this object
>will be true too.

OK, that's good to know, but what if:

srchStr = "!(/Error Count = [1-9]/ || /Abort/ || /[1-9] error/ || /fatal/)" 

which is what it really should be in the case I showed.

Now srchStr doesn't represent a regex, but an expression composed of 
regex's that is eval'ed against $_ to determine whether it is true or not.  
In this case, because of the leading '!' we don't want to see any of the 
strings denoted by the regex's in the equation between the parens.

>
>This doesn't solve your original problem though. It just makes the
>example almost complete nonsense. 
>Everything done there is reading the
>whole file line by line and setting (failed,found = false, true) while
>processing each line, regardless of the content. Then i is incremented
>and everything is done again and again and again....


True, but what if I change my example such that the srchStr changes to 
what I have above:
###############################################################
i = 0
failed = true
found  = false
while true do
   i += 1
   puts i
         rptFile = "CYPsw00695.rpt"
         #the following line is different:
         srchStr = "!(/Error Count = [1-9]/ || /Abort/ || /[1-9] error/ || 
/fatal/)"
         rpt_h = File.open rptFile
         puts "after File.open #{rptFile}" if $DEBUG
         puts "srchStr is: #{srchStr}" if $DEBUG
         while rpt_h.gets do
           #puts "while: #$_" if $DEBUG

           if eval srchStr
              failed = false
              found  = true
           else
              failed = true
              break
           end  #if eval

         end
         rpt_h.close
end         
##########################################################

Actually, I tried this example and it's even worse - it just hangs after 
the sixth iteration.  It is apparently due to the eval.

>
>I have no clue what resources are hogged by this, but if you explain a
>little bit more what you want to do here, we might help you to find a
>more natural ruby approach to it.
>

OK, I've got a test system currently implemented in Perl that I'm trying 
to turn into a distributed application with Ruby.  There are several thousand 
testcases in the system and each testcase has a file associated with it 
that has information about how to run the testcase and how to determine if 
it passed or failed.  To determine pass/fail, there is a string in the 
file (which is the srchStr in the example above) that is an expression 
composed of regex's and logical operators, like:

"!(/Error Count = [1-9]/ || /Abort/ || /[1-9] error/ || /fatal/)"

In perl we iterate through each line of the file and eval the srchStr 
($srchStr in Perl) against $_ (basically: if eval $srchStr { 'do blah'})
it works pretty nicely.  In Ruby that eval hangs after a while.  Now, the 
way I'm doing it may not be the best way to do it, but I don't think the 
eval should hang (and I kind of have to do it this way for historical 
reasons - I really don't want to change those thousands of files out 
there!).

Phil