V
victor.reyes
Note: parts of this message were removed by the gateway to make it a legal Usenet post.
Team,
I would like to initialize some variables/arrays in a method, and access
them within another method.
For instance,
class My_Class
def init_method(size)
arr = Array.new(size)
index = 1
end # End method
def print_method
p arr
p index
end # End method
end # End class
my_obj = My_Class.new()
my_obj.init_method 10
my_obj.print_method
When I attempt to execute this code, I receive the error:
ruby My_Class
My_Class:9:in `print_method': undefined local variable or method `arr' for
#<My_Class:0x28ee87c> (NameError)
from My_Class:17
I know the reason why it is happening, but I don't know how to access a
variable from another method, if there is a way.
Thank you
Victor
Team,
I would like to initialize some variables/arrays in a method, and access
them within another method.
For instance,
class My_Class
def init_method(size)
arr = Array.new(size)
index = 1
end # End method
def print_method
p arr
p index
end # End method
end # End class
my_obj = My_Class.new()
my_obj.init_method 10
my_obj.print_method
When I attempt to execute this code, I receive the error:
ruby My_Class
My_Class:9:in `print_method': undefined local variable or method `arr' for
#<My_Class:0x28ee87c> (NameError)
from My_Class:17
I know the reason why it is happening, but I don't know how to access a
variable from another method, if there is a way.
Thank you
Victor