How to return an unmodifiable but nor frozen array?

  • Thread starter LAMBEAU Bernard
  • Start date
L

LAMBEAU Bernard

Hi,

Is there a mean to obtain an unmodifiable array which is not frozen?

Assume you're implementing a directed graph with adjacency lists. A
node would like to expose it's outgoing edges (to let users enjoy its
enumerability for example) but you would like to ensure that this
array will not be modified. A solution would be to duplicate it and I
know that efficiency should not be my concern, but ...

blambeau
 
M

Markus Schirp

You can return the array in a container, this container redirects all
"read" operations to the array and does not redirect any "write" operations.

class ArrayContainer
def initialize(array)
@array
end

def [](idx)
@array[idx]
end

... and so on
end

There is a specialized "delegate" class/module in ruby, using it should
result in better performance than the solution in my ArrayContainer
class.

Mfg

Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top