class arrays

M

MrZombie

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
 
P

poseid

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.


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
 
P

poseid

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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top