tags:

views:

46

answers:

1

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.

+2  A: 

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.

elliottcable
@elliottcable, thanks a lot.
eric2323223
No problem. If you've got more Ruby questions, you should join #Ruby on the Freenode IRC network. I've also got a channel where I occasionally help, #RubyNoob - feel free to join it as well d-:
elliottcable