eric2323223
eric2323223

Reputation: 3628

Source code problem of ZenTest

Here is one method that monkey patched the Dir[] method from autotest

class Dir
  class << self
    alias :old_index :[]
    def [](*args)
      $-w, old_warn = false, $-w
      old_index(*args)
    ensure
      $-w = old_warn
    end
  end
end

Could you please help by explain this line $-w, old_warn = false, $-w ? Thanks in advance.

Upvotes: 0

Views: 100

Answers (1)

ELLIOTTCABLE
ELLIOTTCABLE

Reputation: 18038

You can assign multiple variables to multiple values on one line in Ruby.

That line is equivalent to the following:

old_warn = $-w
$-w = false

If you were asking what the purpose was; $-w is a global variable in Ruby that points to a boolean that indicates whether or not the user passed the -w flag to the ruby executable when they ran the script. In other words, the variable indicates whether or not the script/program is currently supposed to be printing "warnings".

Essentially, the purpose of that entire block of code is to ensure that warnings are turned off before executing it's core. The old value of the warn flag is saved into a new variable; the warn flag is turned off; and then when the execution is done, the warn flag is re-set back to whatever it used to be.

Upvotes: 3

Related Questions