"ahoward" <ahoward / fsl.noaa.gov> schrieb im Newsbeitrag
news:Pine.LNX.4.53.0305021830390.7377 / eli.fsl.noaa.gov...
> On Fri, 2 May 2003, Robert wrote:
>
> > Err, why are you surprised?  This is exactly what I would have
expected
> > since $defout, $stdout and STDOUT initially point to the same
instance.  So
> > it doesn't really matter on which of the instances you do issue
'reopen',
> > they "all" get redirected.
>
> i'm suprised since STDOUT has the semantics of being a constant (i
realize
> that the object it points to is not constant).  i would have *thought*
that
> changing $stdout (a *variable*) would not of changed STDOUT.  all i'm
saying
> is of what value (in terms of POLS) is it to provied the constant STDOUT
*and*
> the variable $stdout, when in fact *both* are 'variable'.  i'm
completely
> understand what's going on, but think it violated POLS for most people.

The important thing to remember is that variables and constants are
references (like in Java for types deriving from Object). The constness of
a variable is restricted to the reference, i.e., a constant always points
to the same instance.  You can of course freeze that instance in order to
achieve something similar to a real constant, but the semantics of freeze
are type dependent.  And in the case of a stream freezing does not seem to
be of much use either...

> it's simpler in *this* case, but i would maintain that adding to class
IO is
> good because
>
> * IO objects *should* (IMHO) have a redirect method - it is a common
>   operation.

Maybe.

> * adding code to class IO is more *generic* that the above:
>
>   #!/usr/bin/env ruby
>   require 'redirect.rb'
>
>   bitbucket = open '/dev/null', 'w'
>   $stderr.redirected(bitbucket) do
>     system 'ls non-existent-file'
>   end
>
>   it is often the case that > 1 fd needs redirected

That as easily done with begin - ensure.

> * easier to read (see above)

Granted.

> * safer - ensure cannot be forgetton by programmer

??? Sorry, I don't understand this.  Did you mean to say "*can* be
forgotten"?

> * class derived from IO will also have this behavior

Granted.

> * it is more flexible:
>
>   log = open 'log', 'w'
>
>   $stdout.redirected(log) do
>     ...
>     special_log = open 'special', 'w'
>     $stdout.redirected(special_log) do
>       ...
>       extra_special_log = open 'special', 'w'
>       $stdout.redirected(extra_special_log) do
> ...
>       end
>     end
>   end

You can do that as well with begin - ensure.

> * it is 10 lines of code!

Yes, but the crucial part was missing in your posting. (see below)

> anyhow - i can appreciate your opinion but i'm using it in my scripts
;-)  but
> then again i *love* the entire
>
>   Resource.aquire do |resource|
>     ...
>   end
>
> paradigm and the way it makes code read - to me it feels somehow more
> ruby-ish.

But in that case your method definition should have looked differnt. At
least your redirect method does not test for a block, does not invoke the
block and does no cleanup if there is a block.

But that's the nice thing about ruby, that there are often many good ways
to a solution and there's something for everybody. :-)

Kind regards

    robert