Robert Dober wrote:
What about fine graining the control to the closure?
Doesn't really matter, so long as it can be read and duplicated
satisfactorily to fool the rest of the module. And your method doesn't quite
work --
How can it? I did not test it! I guess I have hidden the local
variable with my method :blah
The important thing to retain is IMHO.
Closures are the only way to hide your data in Ruby. I make this
statement without applying any judgment if one shall or shall not :).
I assume you were wanting blah to return something with methods
that restrict access to the scope'd blah?
No not quite, that is why I dupfroze it.
As I have to keep blah mutable, for the purpose of the general
exercise I cannot expose it directly and I have to intercept all
mutable method calls.
I will try to be clearer this time.
class MyBlah
_blah = %w{ the human brain is a wonderful thing. it starts working
before you are born and continues to work
up to the moment you post to ruby talk }
extend( Module::new do
define_method :blah do _blah.dup.freeze end # We will not care
about errors in this toy example
# the forwarder method was an unnecessary
# complication, omitted
define_method :an_access_example do |*args, &blk| # Ruby1.9
# And here goes all our data encapsulation logic before
actually changing _blah
end
end )
end
Of course, this prevents the internal array from being modified, so I see
the point -- if there are other methods sharing the same scope. Even then,
it's still possible to obliterate the whole thing,
I am not sure to understand? What do you mean by scope? Of course one
can access _blah by editing the source ;). But that is the *only* way.
Reopening the class will not give access to _blah.
Cheers
Robert
···
On Sun, Feb 8, 2009 at 10:42 PM, David Masover <ninja@slaphack.com> wrote:
On Sun, Feb 8, 2009 at 9:48 PM, David Masover <ninja@slaphack.com> wrote:
--
It is change, continuing change, inevitable change, that is the
dominant factor in society today. No sensible decision can be made any
longer without taking into account not only the world as it is, but
the world as it will be ... ~ Isaac Asimov