Class variables

Hey guys,

I'm a newbie to programming and Ruby. I'm just learning about Classes
and wondering about instance variables inside classes.

I'm in the process of making a trading card game and I have a class
called FieldController. Now each player has at least two fields so I
have a total of 4 different instances? of this class.
For example:
class FieldController
  @@space1 = []
  @@space2 = []
  @@space3 = []
  @@space4 = []
  @@space5 = []
  def addcard(para)
    ...
  end
  def removecard
  def list
  etc
end

Problem: Having them as class variables doesn't work when there is
multiple FieldControllers... Instance variables don't seem to work
because I can't access them from within the methods such as addcard.
Normal variables are the same.
Questions:
1. How do I create variables that are specific to that particular
instance of FieldController and can be accessed inside the methods.
2. How can I access the variables from within an object without making a
method as an interface, such as:
$field1 = FieldController.new
puts $field1.space1

The answer is going to be super obvious but I'm a newb so..

Thanks for your time
Joshua

···

--
Posted via http://www.ruby-forum.com/.

Hey guys,

I'm a newbie to programming and Ruby. I'm just learning about Classes
and wondering about instance variables inside classes.

I'm in the process of making a trading card game and I have a class
called FieldController. Now each player has at least two fields so I
have a total of 4 different instances? of this class.
For example:
class FieldController
@@space1 =
@@space2 =
@@space3 =
@@space4 =
@@space5 =
def addcard(para)
   ...
end
def removecard
def list
etc
end

Problem: Having them as class variables doesn't work when there is
multiple FieldControllers... Instance variables don't seem to work
because I can't access them from within the methods such as addcard.
Normal variables are the same.
Questions:
1. How do I create variables that are specific to that particular
instance of FieldController and can be accessed inside the methods.

Use instance variables, not class variables.

class FieldController
  def initialize
    @space1 =
    @space2 =
    @space3 =
    @space4 =
    @space5 =
  end
  def addcard(para)
  end
end

Now, your numbering of variables is unusual. It might be better to have a
variable, @spaces which contains a five element array.

2. How can I access the variables from within an object without making a
method as an interface, such as:
$field1 = FieldController.new
puts $field1.space1

Variables are not directly exposed, there must always be a method that sets
/ gets the value. Your example, however, is how things currently work, so
maybe I don't understand? (as an aside, when you create a variable with a
$dollar_sign, it becomes globally visible, so make sure this is _really_
what you want -- if it is, it likely shouldn't be).

class FieldController
  # define the getter
  def space1
    @space1
  end

  # define the setter
  def space1=(new_space)
    @space1 = new_space
  end

  def initialize
    # invoke the setter
    self.space1 = 'space one'
  end
end

# `field1` is a local variable, `$field1` would be global
field1 = FieldController.new
puts field1.space1
# >> space one

···

On Sat, Jan 28, 2012 at 8:11 PM, josh j. <jinjosh97@gmail.com> wrote:

Thanks for your response so quickly!

1. I was using instance variables but used class variables in the
example to demonstrate the problem about multiple instances of
FieldController.
2. I forgot to put the initialize method around the variables in the
example
3. To make it easy to read I re wrote it as simply as possible(cutting
most methods from it). Each Space1,2 etc has 3 values, all the variables
are then inside another variable called @allspaces (by mistake omitted
this)
4. I'm not really sure if I need to use a global variable but I'll try
not using it.
5. I'm not even sure what wasn't working because I was doing pretty much
everything you said but I re wrote it and now it works. Before when I
tried to go "field1.addcard(parameters)" it would do this (simplified)
@allspaces.each{|space|
if space[0] == 0
space[0] = 1
end
}
but after this I would try outputting @allspaces and no vales were
changed, now it works!

Regards Joshua

···

--
Posted via http://www.ruby-forum.com/.

Variables are not directly exposed, there must always be a method that
sets / gets the values

  you could use attr_accessor -

class FieldController
  attr_accessor :space1, space2

  def initialize
    @space1 =
    @space2 =
  end

end

  then....

fc = FieldController.new
p fc.space1
fc.space1 << "hi there"
p fc.space1

  hth-

  - j

···

--
Posted via http://www.ruby-forum.com/\.

attr_accessor doesn't get around this, it writes methods that access the
instance variables. In fact, it writes the same methods that I did (I wrote
them out intentionally to make the relationship between the variable and
the method more apparent).

···

On Sun, Jan 29, 2012 at 1:33 PM, jake kaiden <jakekaiden@yahoo.com> wrote:

> Variables are not directly exposed, there must always be a method that
> sets / gets the values

you could use attr_accessor -

Unsubscribe

Saludos,

···

El 29/01/2012, a las 19:37, "jake kaiden" <jakekaiden@yahoo.com> escribió:

Variables are not directly exposed, there must always be a method that
sets / gets the values

you could use attr_accessor -

class FieldController
attr_accessor :space1, space2

def initialize
   @space1 =
   @space2 =
end

end

then....

fc = FieldController.new
p fc.space1
fc.space1 << "hi there"
p fc.space1

hth-

- j

--
Posted via http://www.ruby-forum.com/\.