# Having problems with my instance variable

**URL:** <https://www.ruby-forum.com/t/having-problems-with-my-instance-variable/128110>\
**Category:** Ruby\
**Created:** [February 5, 2008, 3:15am UTC](https://www.ruby-forum.com/t/having-problems-with-my-instance-variable/128110 "2008-02-05T03:15:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![macaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@macaco](https://www.ruby-forum.com/u/macaco)\
**Post date:** [February 5, 2008, 3:15am UTC](https://www.ruby-forum.com/t/having-problems-with-my-instance-variable/128110/1 "2008-02-05T03:15:01Z")

</div>

I’m sorry if this question seem stupid, but couldn’t find it in the  
books.

I have this code in the models directory:  
class Figure  
attr\_reader :xpos, :ypos

def initialize  
@xpos = 0  
@ypos = 0  
@other = 0  
@another = 0  
end

def some  
call\_other(@xpos,@ypos)  
end

def more  
call\_other(@other,@another)  
end

def call\_other(var1, var2)  
max = 9  
var1= rand(max)  
var2=rand(max)  
end  
end

And have this on my controller:

def figure  
fig = Figure.new  
fig.some  
render :text =\> “

# new values
”+fig.xpos.to\_s+"  
"+fig.ypos.to\_s  
end

Problem is, it always print 0 0… as if the random wouldn’t work, but I  
have test the random outside this method (calling it in the first  
method,  
not the second one), but the second method is one I need cos I use it  
too  
many times and with different instance variables

Any help?

---

<div class="post-metadata">

**Author:** ![macaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@macaco](https://www.ruby-forum.com/u/macaco)\
**Post date:** [February 5, 2008, 4:30am UTC](https://www.ruby-forum.com/t/having-problems-with-my-instance-variable/128110/2 "2008-02-05T04:30:44Z")

</div>

On Feb 4, 2008 8:14 PM, macaco [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> var1= rand(max)  
> var2=rand(max)  
> end  
> end

It’s not clear to me what you’re trying to do here. You want a method  
that  
will set arbitrary variables to random numbers? That’s a wild guess.  
What  
you’re actually doing when you call call\_other is passing it the values  
in  
@xpos and @ypos (which is 0 and 0 initially). Then, in call\_other,  
you’re  
taking those 0s in with var1 and var2. Then you’re over-writing var1 and  
var2 with random numbers (0s are lost, as far as this method is  
concerned)  
and then… you exit. You’re leaving your @vars untouched because you’re  
passing the values, not the variables themselves.

If “max” is always going to be the same, I’d make it a constant at the  
class  
level and then just have some look like this:

def some  
@xpos = rand MAX  
@ypos = rand MAX  
end

If max won’t be the same all the time, make a method that figures it out  
and  
returns a random number based on it… An example will make more sense,  
maybe.

def some  
@xpos = generate\_random  
@ypos = generate\_random  
end

def generate\_random  
max = 9 # or whatever determines your max value  
rand(max)  
end

I hope that’s clear. Scope can be confusing sometimes. If I am too  
rambly (I  
hear that a lot), maybe someone with more experience will be more clear,  
too.

Ben

---

<div class="post-metadata">

**Author:** ![macaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@macaco](https://www.ruby-forum.com/u/macaco)\
**Post date:** [February 5, 2008, 5:03am UTC](https://www.ruby-forum.com/t/having-problems-with-my-instance-variable/128110/3 "2008-02-05T05:03:43Z")

</div>

> def some  
> call\_other(@xpos,@ypos)  
> end
> 
> def more  
> call\_other(@other,@another)  
> end

This worked:  
class Test  
attr\_reader :xpos

def initialize  
@xpos = 0  
end

def some  
p “before call: #{@xpos}”  
#notice the colon  
c :@xpos  
p “after call: #{@xpos}”  
end

def call\_other(var)  
max = 9  
self.instance\_variable\_set(var, rand(max))  
end  
end

begin  
t = Test.new  
t.some  
end

wyz@local ~ $ ruby test.rb  
“before call: 0”  
“after call: 7”

wyz@local ~ $ ruby test.rb  
“before call: 0”  
“after call: 2”

Warning: I’m not feeling well and the NyQuil is kicking in… 😕

---

<div class="post-metadata">

**Author:** ![macaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@macaco](https://www.ruby-forum.com/u/macaco)\
**Post date:** [February 5, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/having-problems-with-my-instance-variable/128110/4 "2008-02-05T18:37:47Z")

</div>

Thanks Day, but this is just a simplification of what I need

@Andrew S., thanks instance\_variable\_set makes it’s work, but what if  
i  
need the data inside too… for example

def call\_other(var1,var2)  
max = 8  
if(var1 == 0)  
var1 = rand max  
else  
var1 += max  
end  
var2 = rand max  
end

BTW, can you explain the line of code “c :@xpos”, didn’t get it

Thanks again

---

<div class="post-metadata">

**Author:** ![macaco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@macaco](https://www.ruby-forum.com/u/macaco)\
**Post date:** [February 5, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/having-problems-with-my-instance-variable/128110/5 "2008-02-05T20:16:34Z")

</div>

On Feb 5, 2008 11:37 AM, macaco [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @Andrew S., thanks instance\_variable\_set makes it’s work, but what if i  
> need the data inside too…

I think instance\_variable\_get? Check out [RDoc Documentation](http://www.ruby-doc.org/core/)  
and  
look in the right column for methods that start with instance\_v. Or do a  
find on \_variable and see what turns up.

Ben
