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
encapsulation issue
by James French other posts by this author
Nov 6 2009 5:53AM messages near this date
Re: [ANN] Ruby-Tk LIbraries Runtime Library 3.0 and Gestalt Items | Re: encapsulation issue
Hi,

Is there any way of providing read only access to an array? (problem shown by code below).

class A

  def initialize
    @dependencies = []
  end

  # intended to be read only access
  def dependencies
    @dependencies
  end

  def addDependency(d)
    @dependencies << d
    puts "adding #{d}"
  end
end


a = A.new
a.addDependency("foo")
a.dependencies << "bar" # encapsulation subverted

puts a.dependencies   # foo and bar both in array


Any suggestions appreciated,
James
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