On Mon, Jul 1, 2013 at 6:40 PM, Joel VanderWerf <joelvanderwerf / gmail.com>wrote:

> On 07/01/2013 09:22 AM, Bryan Lockwood wrote:
> ...
>
>  My problem was that I read about fileutils in the pickaxe book (for 1.9
>> and 2.0), and the example included FileUtils::Verbose,
>> and I figured that would be fine for starters, as I could then see what
>> it was doing.
>>
>
> That example[1] seems to have been written with brevity in mind. It's ok
> in a 10 line script to include modules globally. In general, one must be
> careful, for reasons like the one you found. The internal state managed by
> those modules can interact badly with the internal state of other classes.
> It's better, though more verbose, to call methods on the module:
>
> FileUtils.cp(....)
>
> An exception would be if you were designing a class as a command language,
> like rake. But in that case, you would only include FileUtils into that
> class, not globally.
>

Even there I probably wouldn't.  Here's a shortened version of what happens:

$ ruby x.rb
[]
[:@var]
$ cat x.rb

module X
  def bar
    @var = 123
  end
end

include X

class Y
  def m
    bar
  end
end

y = Y.new
p y.instance_variables
y.m
p y.instance_variables
$

This shows the dangers of including modules.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/