ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: encapsulation issue
by Gavin Sinclair other posts by this author
Nov 6 2009 3:56PM messages near this date
Re: encapsulation issue | Re: encapsulation issue
On Nov 7, 12:50 am, James French <James.Fre...@[...].com> 
wrote:
> 
>  Is there any way of providing read only access to an array? [...]
> 
> 
>  a = A.new
>  a.addDependency("foo")
>  a.dependencies << "bar" # encapsulation subverted

Ask yourself if it's really necessary to implement #dependencies.
(The answer may be 'yes'; I'm just saying don't assume it is.)

For instance, you could provide access to the dependencies like this

  class A
    # initialise and add_dependency as before

    include Enumerable
    def each(&block); @dependencies.each(&block); end

    def [](n)
      @dependencies[n]     # possibly .dup this
    end
  end

  a = A.new
  a.add_dependency "foo"
  a.each do ... end
  a.map { ... }
  a.grep(...)
  a[5]

I've often found, when wrapping a basic data type, that I don't need
access to the 'guts' of that type; I only need to do a certain set of
things, so I implement them and leave it at that.

--
Gavin Sinclair
Thread:
James French
Josh Cheek
James French
Ralph Shnelvar
Gavin Sinclair
James French
Seebs
Aldric Giacomoni
David A. Black
James French
David A. Black
Tom Stuart
James French

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved