From: Robert Klemme Date: 2013-07-02T17:57:05+09:00 Subject: Re: including FileUtils::Verbose crashes Marshal.dump --047d7b6da78254fbda04e083876d Content-Type: text/plain; charset=ISO-8859-1 On Mon, Jul 1, 2013 at 6:40 PM, Joel VanderWerf 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/ --047d7b6da78254fbda04e083876d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Mon, Jul 1, 2013 at 6:40 PM, Joel VanderWerf &= lt;joelvander= werf@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 o= k 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 cla= ss, not globally.

Even there I pr= obably wouldn't. =A0Here's a shortened version of what happens:

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

module X
=A0 d= ef bar
=A0 =A0 @var =3D 123
=A0 end
end
=

include X

class Y
=A0 def m
=A0 =A0 bar
=A0 end
end

y =3D 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.rubybestpractice= s.com/
--047d7b6da78254fbda04e083876d--