Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

How to edit instance variables? (><)

Haki

Member

You need to put "a" as a attr_accessor. Then you can do it like this:
Code:
class Test_A
attr_accessor   :a

def main
   @a = 2
end
end

class Test_B

def main
   Test_A.a = 1
end
end
 

Khoa

Member

Can you give me another example, I get an error:

Script '' line 11: NoMethodError occurred.
undefined method `a=' for Test_A:Class

This is line 11:
Code:
Test_A.a = 1
 

khmp

Sponsor

Wait let's stop for a second so we can get off on the right foot. Variables preceded by a single "@" are known as instance variables. Variables that are prefixed with two "@". Like this, @@test = 0, are known as class variables. Now let's say you have an instance variable in one class and would like to manipulate it from inside another class. There are two ways that I can think of off the top of my head. The second class has an instance of the first class within it at all times, through that instance you can manipulate the attribute you want from the first class. Or the second class (inherits/derives/is the child of) the first class.

Here's what I mean by having an instance of the first class within the second class.
Code:
class Test_A
  attr_accessor :a
  def initialize
    @a = 2
  end
end

class Test_B
  attr_accessor :test
  def initialize
    @test = Test_A.new
  end
end

test_instance = Test_B.new
test_instance.test.a = 5

Now let's see it using inheritance.
Code:
class Test_A
  attr_accessor :a
  def initialize
    @a = 2
  end
end

class Test_B < Test_A
  def initialize
    super # Call Test_A's initialize
  end
end

test_instance = Test_B.new
p test_instance.a # Prints "2"
test_instance.a = 5

Good luck with it Khoa! :thumb:
 

Zeriab

Sponsor

If you want to use Test_A.a then you must define class methods and using class variables is a good idea as well.
You can do it like this:
Code:
class Test_A
  def self.a
    @@a = 1
  end

  def self.a=(value)
    @@a = value
  end
end

*hugs*
- Zeriab
 

Khoa

Member

@khmp: does that mean I can only edit instance variables outside the class, not inside?
@ Zeriab: Perhap I have to use class variables like your idea if there isn't other way.
 

khmp

Sponsor

Khoa":2tr01fby said:
@khmp: does that mean I can only edit instance variables outside the class, not inside?
@ Zeriab: Perhap I have to use class variables like your idea if there isn't other way.

No. Instance variables can be changed from within the class as well as. And don't consider it outside the class when changing it through an instance of a class. I think you're tying the wrong meaning to the terms "instance variables" and "class variables". What makes them different? Scope is differentiating factor. Instance variables are called instance variables because they exist while an instance of the class they are contained within exists.

For Example:
Code:
class Test
  attr_accessor :a
  def initialize
  end
end

temp = Test.new
temp.a = 'A exists'
p temp.a # prints "A exists"
temp = nil # '@a' no longer exists when the instance of the Test class no longer exists.

Now what about class variables? Class variables have scope over the entirety of the class' existence. Meaning the whole time the program is running these guys will be available and have scope. Usually as Zeriab has modeled above we write class methods to access the class variables because we usually want access to them without having to create an instance of the class. I know that sounds a little confusing so think of it like this. Let's say you had a piece of data that would only occasionally need changing within a class or what if you wanted to find out how many instances of a particular class were created. You would use a class variable. As I'm almost certain I did a poor job of explaining their purpose. I would suggest searching for a Ruby Tutorial on variable scope.

Reference Counting:
Code:
class Test
  @@instances = 0
  def initialize
    @@instances += 1
  end
  def self.instances_remain
    return @@instances
  end
  def dispose
    @@instances -= 1
  end
end

Test.new.dispose
Test.new.dispose
Test.new # No disposal should leave us with a count of 1
p Test.instances_remain # 1

The main point of the code above was to show case that the variable isn't destroyed when the class dies or has no scope. If you didn't dispose of any of those classes your finishing number would be 3 instead of one. And feel free to play around with that code. But class variables are very rarely used. They can create some un/wanted side effects through inheritance and can have issues with scope. Instance variables are your bread and butter when coding in Ruby/RGSS.

Good luck with it Khoa! :thumb:
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top