How to return an unmodifiable but nor frozen array?

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

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
@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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs