-1

Ruby newb here. I am trying to create a class that has a array as its attribute and I want to add elements to a method of the class. I don't know the elements of the array beforehand so I can't do something like this

So far I have tried the following but it doesn't work

class Test
    attr_accessor :test_string
    def initialize
        @test_string = []
    end
    def test_if_array
        test_string << "Foobar" #doesn't work
        test_string.push("Barbaz") #doesn't work
        @test_string.push("Bla") #doesn't work
        self.test_string << "foo" #doesn't work
        self.test_string.push("bar") #doesn't work
    end
end

tester =  Test.new
p tester.test_string

Can anyone tell me how to push strings in the array programatically?

1 Answer 1

1

You didn't call the method. Call tester.test_if_array

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

2 Comments

<sarcasm> I am genius </sarcasm>
It happens to the best of us! :)

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.