1

Here's a ruby script:

discarded_rows=-1

def foobar(line)

    if line.match('records discarded: *(\d+)') then
       discarded_rows=$1;
       puts('yepp, I have found: ' + discarded_rows);
    end;

end


foobar('records successful:    99    ');
foobar('records discarded:      2    ');
foobar('records unknown:        8    ');

if discarded_rows != 2 then
   puts("discarded_rows: #{discarded_rows}");
end

And here's what I believe it does: it declares a (global) variable with the name discarded_rows. It then declares a function foobar that checks if the passed argument line matches "records discarded *\d". If it does, it assignes the number of discarded records to the (what I think is a global) variable discarded_rows. If it matches, it also prints "yepp...." just to make sure that the match works.

The function is called, with one string that should match.

If discarded_rows is not equal to 2, it prints the according value.

And here's the script's output:

yepp, I have found: 2
discarded_rows: -1

So, clearly, the match worked, and obviously, discarded_rows is not truly global. Is this correct or do I overlook something?

0

2 Answers 2

8

discarded_rows is not a global variable. $discarded_rows would be a global variable.

Sign up to request clarification or add additional context in comments.

Comments

3

Zabba is absolutely correct. To elaborate a little bit however, discarded_rows is a local variable. Code at the top level is treated roughly as though the whole file was being executed as a method. Defining a new method actually happens much more so at runtime than in most languages, to the point where you can write something like

s = gets
if s =~ /(\d+)/
   i = $1.to_i
   if i < 5
     def less_than_five
       true
     end
   else
     def less_than_five
       false
     end
    end
 else
   def less_than_five
     raise "not a number"
   end
end

p less_than_five

The body of an def end block introduces a new lexical scope that does not close over the enclosing lexical scope (locals in the surrounding code are not available to the body of the method).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.