Class arrays

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

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

···

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

Hello,

how does this work?

thanks

--
Thank you for your brain.
-MrZombie

Hello,

how does this work?

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

=> [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

···

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

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

···

On 18 Mai, 16:00, MrZombie <mrzom...@thezombie.net> wrote:

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

···

On 18 Mai, 16:00, MrZombie <mrzom...@thezombie.net> wrote:

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.

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 Nielsen

···

On Tue, May 18, 2010 at 8:15 AM, poseid <mulder.patrick@gmail.com> wrote:

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

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

···

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

On 18 Mai, 16:00, MrZombie <mrzom...@thezombie.net> wrote:

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