Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1060451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:19:44+00:00 2026-05-16T18:19:44+00:00

I want to have an array as a instance variable using attr_accessor . But

  • 0

I want to have an array as a instance variable using attr_accessor.

But isn’t attr_accessor only for strings?

How do I use it on an array?

UPDATE:

Eg. If you want:

object.array = "cat"
object.array = "dog"
pp object.array
=> ["cat", "dog"]

Then you have to create those methods yourself?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:19:44+00:00Added an answer on May 16, 2026 at 6:19 pm

    Re your update:

    Although you can implement a class which acts as you describe, it is quite unusual, and will probably confuse anyone using the class.

    Normally accessors have setters and getters. When you set something using the setter, you get the same thing back from the getter. In the example below, you get something totally different back from the getter. Instead of using a setter, you should probably use an add method.

    class StrangePropertyAccessorClass
    
      def initialize
        @data = []
      end
    
      def array=(value)   # this is bad, use the add method below instead
        @data.push(value)
      end
    
      def array
        @data
      end
    
    end
    
    object = StrangePropertyAccessorClass.new
    
    object.array = "cat"
    object.array = "dog"
    pp object.array
    

    The add method would look like this:

      def add(value)
        @data.push(value)
      end
    
    ...
    
    object.add "cat"
    object.add "dog"
    pp object.array
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which I want to use in a query: Array (
I have an app (using retain/release, not GC) that maintains an NSArray instance variable,
Say I have: class DecisionTree(private val instances: Array[Instance]){ and I want to calculate another
I have a Ruby class. I want to get an instance variable from an
I have variable names stored in an array, and I want to loop through
I want to have an array of ArrayLists: ArrayList<MyClass>[] myArray; I want to initialize
What if i want to have an array of pointers to a function and
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
VISUAL C++ Question Hi, I have array of 3 elements and I want to
i have an array and i want to convert this array in comma seprated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.