# Class arrays

**URL:** <https://www.ruby-forum.com/t/class-arrays/188792>\
**Category:** Ruby\
**Created:** [May 18, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/class-arrays/188792 "2010-05-18T15:40:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![poseid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@poseid](https://www.ruby-forum.com/u/poseid)\
**Post date:** [May 18, 2010, 3:40pm UTC](https://www.ruby-forum.com/t/class-arrays/188792/1 "2010-05-18T15:40:31Z")

</div>

Hello,

how does this work?

> > class A  
> > b = [1, 2,3]  
> > end  
> > =\> [1, 2, 3]
> 
> >

Is b an instance variable? class variable? how would I access b from  
outside the class?

thanks

---

<div class="post-metadata">

**Author:** ![poseid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@poseid](https://www.ruby-forum.com/u/poseid)\
**Post date:** [May 18, 2010, 4:05pm UTC](https://www.ruby-forum.com/t/class-arrays/188792/2 "2010-05-18T16:05:41Z")

</div>

On 2010-05-18 09:38:14 -0400, poseid said:

> Hello,
> 
> how does this work?
> 
> thanks

Erh, not that way. Sorry.

You could have

class A  
@b = [1,2,3]  
end

Which stores the array in a class-level instance variable.

This other one  
class A  
@@b = [1,2,3]  
end

stores the array in a class variable.

An instance variable would (or could, anyway) be used like this

class A  
def initialize  
@b = [1,2,3]  
end

#accessor methods  
def b  
@b  
end  
def b= (value)  
@b = value  
end  
end

Also, you can shorten the accessor methods to

class A  
attr\_accessor :b  
def initialize  
@b = [1,2,3]  
end  
end

---

<div class="post-metadata">

**Author:** ![poseid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@poseid](https://www.ruby-forum.com/u/poseid)\
**Post date:** [May 18, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/class-arrays/188792/3 "2010-05-18T16:17:59Z")

</div>

On 18 Mai, 16:00, MrZombie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This other one  
> class A  
> @@b = [1,2,3]  
> end
> 
> stores the array in a class variable.

Hm… ok, this is more or less what I expected as well, when reading  
code employing instance/class variables. But how do I understand the  
(old) code from a book on Rails:

class T \< ActionView::Helpers::FormBuilder  
field\_helpers.each do |s|  
src = \<\<-END\_SRC  
def #{s}(field, options = {})  
@template.content\_tag( … )  
end  
END\_SRC  
class\_eval src, **FILE** , **LINE**  
end  
end

To me it seems that field\_helpers is an instance variable, but there  
is no @ nor @@ used

thanks for feedback

---

<div class="post-metadata">

**Author:** ![poseid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@poseid](https://www.ruby-forum.com/u/poseid)\
**Post date:** [May 18, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/class-arrays/188792/4 "2010-05-18T16:23:50Z")

</div>

On Tue, May 18, 2010 at 8:15 AM, poseid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Â Â Â END\_SRC  
> Â class\_eval src, **FILE** , **LINE**  
> Â end  
> end
> 
> To me it seems that field\_helpers is an instance variable, but there  
> is no @ nor @@ used
> 
> thanks for feedback

field\_helpers is neither a class nor an instance variable… it is a  
method that happens to return an array (or similar class that has a  
.each).

-Jonathan N.

---

<div class="post-metadata">

**Author:** ![poseid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@poseid](https://www.ruby-forum.com/u/poseid)\
**Post date:** [May 18, 2010, 5:42pm UTC](https://www.ruby-forum.com/t/class-arrays/188792/5 "2010-05-18T17:42:19Z")

</div>

On Tuesday, May 18, 2010 08:40:08 am poseid wrote:

> =\> [1, 2, 3]
> 
> Is b an instance variable? class variable?

Nope. It’s a local variable. That means it disappears as soon as the  
interpreter hits that ‘end’ statement.

> how would I access b from  
> outside the class?

You could create a class variable, but I’d recommend an instance  
variable on  
the class instead:

class A  
class \<\< self  
attr\_accessor :b  
end  
self.b = [1, 2, 3]  
end

p A.b

---

<div class="post-metadata">

**Author:** ![poseid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@poseid](https://www.ruby-forum.com/u/poseid)\
**Post date:** [May 21, 2010, 1:10am UTC](https://www.ruby-forum.com/t/class-arrays/188792/6 "2010-05-21T01:10:33Z")

</div>

On 2010-05-18 15:53:02 +0100, poseid said:

> > Which stores the array in a class-level instance variable.  
> > Hm… ok, this is more or less what I expected as well, when reading  
> > class\_eval src, **FILE** , **LINE**  
> > end  
> > end
> 
> To me it seems that field\_helpers is an instance variable, but there  
> is no @ nor @@ used
> 
> thanks for feedback

I’m not familiar with the old actionview methods, but it strikes me  
that the most likely reason is that field\_helpers is a class method  
that returns an array of strings. Sort of like

def self.field\_helpers  
%w(list of desired words)  
end

or something to that effect. You could check that by grepping the  
methods that T has.

Mac

---

<div class="post-metadata">

**Author:** ![poseid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@poseid](https://www.ruby-forum.com/u/poseid)\
**Post date:** [May 18, 2010, 4:55pm UTC](https://www.ruby-forum.com/t/class-arrays/188792/7 "2010-05-18T16:55:27Z")

</div>

On 18 Mai, 16:00, MrZombie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This other one  
> class A  
> @@b = [1,2,3]  
> end
> 
> stores the array in a class variable.

Hm… ok, this is more or less what I expected as well, when reading  
code employing instance/class variables. But how do I understand the  
(old) code from a book on Rails:

class T \< ActionView::Helpers::FormBuilder  
field\_helpers.each do |s|  
src = \<\<-END\_SRC  
def #{s}(field, options = {})  
@template.content\_tag( … )  
end  
END\_SRC  
class\_eval src, **FILE** , **LINE**  
end  
end

To me it seems that field\_helpers is an instance variable, but there  
is no @ nor @@ used

thanks for feedback
