0

So I have an array (defined right away):

some_array = ["a", "b", "c"]

I am later declaring this class:

class Report
    def initialize(name)
        @name=name
     end
 end

I want to include the array into this class, so I can use the data. Can I import it like any other initialized data? So basically I have an array I want to use in a class.. every way I do it I'm getting undefined variables.

2
  • I'm going to loop through that array in the class, I'm not trying to input the array as arguments, but literally to loop through them inside the class. Commented Sep 1, 2012 at 4:05
  • Have you tried adding an argument to the initialize method and passing the array to that? Commented Sep 1, 2012 at 4:08

1 Answer 1

0

initialize is called when creating an instance of a class. Try

some_array = ["a", "b", "c"]

class Report
  def initialize(name)
    @name=name
  end
end

# Create instance of Report:
report = Report.new some_array

# Check if instance variable @name has been set as expected:
report.instance_variable_get(:@name)

I hope you find this helpful.

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.