# Arrays(setters and getters)

**URL:** <https://www.ruby-forum.com/t/arrays-setters-and-getters/211431>\
**Category:** Ruby\
**Created:** [September 4, 2011, 1:16am UTC](https://www.ruby-forum.com/t/arrays-setters-and-getters/211431 "2011-09-04T01:16:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [September 4, 2011, 1:16am UTC](https://www.ruby-forum.com/t/arrays-setters-and-getters/211431/1 "2011-09-04T01:16:10Z")

</div>

# whats wrong with that

class Whatever  
attr\_accessor :array  
@array  
def initialize  
@array = []  
@array = [“apple”, “banana”, “peach”]  
end  
def array []= (i,value) # setter  
@array[i] = value  
end  
def array[] (i) #getter  
@array[i]  
end  
end

shehio = Whatever.new  
puts shehio.inspect  
puts shehio.array  
puts shehio.array[0]  
puts shehio.array[1]  
puts shehio.array[2]  
shehio.array[0] = 1  
shehio.array[0] = 2  
shehio.array[0] = 3  
puts shehio.array[0]  
puts shehio.array[1]  
puts shehio.array[2]

---

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [September 4, 2011, 1:23am UTC](https://www.ruby-forum.com/t/arrays-setters-and-getters/211431/2 "2011-09-04T01:23:18Z")

</div>

On Sat, Sep 3, 2011 at 6:16 PM, jack jones [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> puts shehio.array[2]  
> shehio.array[0] = 1  
> shehio.array[0] = 2  
> shehio.array[0] = 3  
> puts shehio.array[0]  
> puts shehio.array[1]  
> puts shehio.array[2]
> 
> –  
> Posted via [http://www.ruby-forum](http://www.ruby-forum). [http://www.ruby-forum.com/](http://www.ruby-forum.com/)

whatever.array returns the array object, then whatever.array.=(1,2) is  
invoked on the array object, you can’t define that from your whatever  
class  
unless you define it on the array itself, in the initialize method or  
something.

Not to worry, though, array already implements that interface and it  
does  
what you’re trying to define. Just do this:

class Whatever  
attr\_accessor :array  
def initialize  
@array = [“apple”, “banana”, “peach”]  
end  
end

---

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [September 4, 2011, 1:31am UTC](https://www.ruby-forum.com/t/arrays-setters-and-getters/211431/3 "2011-09-04T01:31:54Z")

</div>

Isn’t there any other way to invoke shehio.array[index] = sth … Can  
this be achieved?

---

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [September 4, 2011, 3:01am UTC](https://www.ruby-forum.com/t/arrays-setters-and-getters/211431/4 "2011-09-04T03:01:11Z")

</div>

hi Shehio,

so, a couple of things…

> # whats wrong with that
> 
> class Whatever  
> attr\_accessor :array  
> @array ### THIS IS NOT NECESSARY  
> def initialize  
> @array = ### THIS IS NOT NECESSARY EITHER  
> @array = [“apple”, “banana”, “peach”]  
> end  
> ### THE FOLLOWING IS NOT NECESSARY  
> def array = (i,value) # setter  
> @array[i] = value  
> end  
> def array (i) #getter  
> @array[i]  
> end  
> end

this code could read:

```
class Whatever

  attr_accessor :array

  def initialize
    @array = ["apple", "banana", "peach"]
  end

end

```

and all of this will work just fine:

shehio = Whatever.new  
puts shehio.inspect  
puts shehio.array  
puts shehio.array[0]  
puts shehio.array[1]  
puts shehio.array[2]  
shehio.array[0] = 1  
shehio.array[0] = 2  
shehio.array[0] = 3  
puts shehio.array[0]  
puts shehio.array[1]  
puts shehio.array[2]

- j

---

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [September 4, 2011, 1:44am UTC](https://www.ruby-forum.com/t/arrays-setters-and-getters/211431/5 "2011-09-04T01:44:13Z")

</div>

On Sat, Sep 3, 2011 at 6:31 PM, jack jones [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Isn’t there any other way to invoke shehio.array[index] = sth … Can  
> this be achieved?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Yes, but not from instances of Whatever. You can fake out the object you  
return from the array method

class Whatever

# this class does nothing that real arrays don’t already do, so…

class FakeArray  
def initialize  
@real\_array = [“apple”, “banana”, “peach”]  
end  
def =(index, val)  
@real\_array[index] = val  
end  
def   
@real\_array  
end  
end

attr\_accessor :array  
def initialize  
@array = FakeArray.new  
end

end

What are you trying to do that you think you need this capability?

---

<div class="post-metadata">

**Author:** ![jackson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jackson](https://www.ruby-forum.com/u/jackson)\
**Post date:** [September 4, 2011, 5:07am UTC](https://www.ruby-forum.com/t/arrays-setters-and-getters/211431/6 "2011-09-04T05:07:38Z")

</div>

When you write this:

class Dog  
@x  
end

you create what’s called a ‘class instance variable’, and you access it  
using the class like this:

Dog.x = 10 #setter  
puts Dog.x #getter

However, just like with regular instance variables, you have to define  
accessor methods in order to access a ‘class instance  
variable’:

class Dog

@dog

def Dog.x=(val)  
@x = val  
end

def Dog.x  
@x  
end

end

Dog.x = 10  
puts Dog.x

–output:–  
10

@ variables attach themselves to whatever self is at the moment. Inside  
a class, but outside any defs, self is equal to the class. Inside a  
def, self is equal to the object that called the method. If that  
doesn’t make any sense, don’t worry about it.
