0

Here is a minimal example of a data structure encapsulated by a class. The data structure should be accessible by class methods. Hence, I want to control the access for example to forbid certain modifications.

    class List
      attr_accessor :array

      def initialize
        @array = ["b"]
      end

      def addElement(v)
        @array.insert(v)
      end
    end


    a = List.new
    puts a.array.to_s
    a.addElement("a")
    puts a.array.to_s

The output is

["b"]
["b"]

The problem occured while using the graph theory gem plexus. The above is just a minimal example. I am trying to learn Ruby from a Java background.

1 Answer 1

1

You're using Array#insert wrong. Consider using Array#<< or Array#push instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.