# How do I make variables defined in a method accessible on another method?

**URL:** <https://www.ruby-forum.com/t/how-do-i-make-variables-defined-in-a-method-accessible-on-another-method/119164>\
**Category:** Ruby\
**Created:** [November 4, 2007, 4:51am UTC](https://www.ruby-forum.com/t/how-do-i-make-variables-defined-in-a-method-accessible-on-another-method/119164 "2007-11-04T04:51:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [November 4, 2007, 4:51am UTC](https://www.ruby-forum.com/t/how-do-i-make-variables-defined-in-a-method-accessible-on-another-method/119164/1 "2007-11-04T04:51:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [November 4, 2007, 5:22am UTC](https://www.ruby-forum.com/t/how-do-i-make-variables-defined-in-a-method-accessible-on-another-method/119164/2 "2007-11-04T05:22:55Z")

</div>

Am Sun, 4 Nov 2007 12:51:19 +0900  
schrieb “Victor R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> end # End method
> 
> ```
> 
> end # End class
> 
> my\_obj = My\_Class.new()  
> my\_obj.init\_method 10  
> my\_obj.print\_method

Use instance variables!

class Example  
def init(size) # why u arent using initialize?  
@arr = Array.new  
@index = -1  
end

def print\_method  
p @arr  
p @index  
end  
end # class Example

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [November 4, 2007, 5:38am UTC](https://www.ruby-forum.com/t/how-do-i-make-variables-defined-in-a-method-accessible-on-another-method/119164/3 "2007-11-04T05:38:06Z")

</div>

On 11/4/07, Victor R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Team,
> 
> 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.

class My\_Class  
def initialize(size)  
@arr = Array.new(size)  
@ind = 1  
end

def print\_method  
p @arr  
p @ind  
end  
end

my\_obj = My\_Class.new(10)  
my\_obj.print\_method

Harry

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [November 4, 2007, 8:47am UTC](https://www.ruby-forum.com/t/how-do-i-make-variables-defined-in-a-method-accessible-on-another-method/119164/4 "2007-11-04T08:47:41Z")

</div>

Markus S. wrote:

> class Example  
> def init(size) # why u arent using initialize?  
> @arr = Array.new  
> @index = -1  
> end

Just to flesh out that comment. Examine this class:

class Dog  
def init\_method(age, weight)  
@age = age  
@weight = weight  
end

def show  
puts @age, @weight  
end  
end

d = Dog.new  
d.init\_method(2, 20)  
d.show

–output:–  
2  
20

However, when you create an object, ruby _automatically_ calls a method  
called initialize() in the object’s class:

class Dog  
def initialize(age, weight)  
@age = age  
@weight = weight  
end

def show  
puts @age, @weight  
end  
end

d = Dog.new(2, 20)  
d.show

–output:–  
2  
20

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [November 4, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/how-do-i-make-variables-defined-in-a-method-accessible-on-another-method/119164/5 "2007-11-04T14:36:22Z")

</div>

First, thank you to Markus, Harry and 7stud for your help.  
I truly appreciate it.

Markus, answering your question “why u arent using initialize?” SIMPLE:  
I  
didn’t know how to use it.

Again, Thank you all!

Victor
