# Class variable as parameter

**URL:** <https://www.ruby-forum.com/t/class-variable-as-parameter/121109>\
**Category:** Ruby\
**Created:** [November 23, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109 "2007-11-23T22:20:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Filipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@Filipe](https://www.ruby-forum.com/u/Filipe)\
**Post date:** [November 23, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109/1 "2007-11-23T22:20:56Z")

</div>

I have the following situation:

class MyClass  
def setVar(var,varValue)  
var=varValue  
end

def initialize  
@myVar=0  
setVar(@myVar,1)  
puts(@myVar.to\_s)  
end  
end

test = MyClass.new

Unfortunately, this piece of code is returning 0 instead of the 1 I  
would expect. Is there anything I could do?

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![Filipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@Filipe](https://www.ruby-forum.com/u/Filipe)\
**Post date:** [November 23, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109/2 "2007-11-23T23:01:20Z")

</div>

On Nov 23, 2007, at 4:20 PM, Filipe wrote:

> ```
> puts(@myVar.to_s)
> 
> ```
> 
> end  
> end
> 
> test = MyClass.new
> 
> Unfortunately, this piece of code is returning 0 instead of the 1 I  
> would expect. Is there anything I could do?

Your call to setVar can not change the value of an variable. Only  
values of variables are passed in Ruby, not the variables themselves.

```
def setVar(var,varValue)
   var=varValue # <-- this is the same as: return varValue
end

def initialize
    @myVar=0
    setVar(@myVar,1) # <-- at this point this is the same as:

```

setVar(0,1)  
puts(@myVar.to\_s)  
end

Ruby has a built-in method, Object#instance\_variable\_set that you can  
use for situations where you need to assign by name, so you don’t  
need to define it for any derived class. Also, Ruby can define  
accessor methods for you:

class MyClass  
attr\_accessor :myVar  
def initialize  
@myVar=0  
self.myVar = 1  
puts myVar # \<-- to\_s not needed; puts applies to\_s to objects  
end  
end  
test = MyClass.new

This will output 1 and may be what you are looking for.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![Filipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@Filipe](https://www.ruby-forum.com/u/Filipe)\
**Post date:** [November 24, 2007, 12:25am UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109/3 "2007-11-24T00:25:37Z")

</div>

On 23 nov, 20:00, Morton G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > would expect. Is there anything I could do?  
> > setVar(@myVar,1) # ← at this point this is the same as:  
> > attr\_accessor :myVar  
> > Regards, Morton  
> > The key point is that the setVar method is supposed to do a couple  
> > more things than just set the value of the variable. I removed them  
> > from the example just for the sake of simplicity. Something like:

class MyClass  
def setVar(var,varValue)  
var=varValue  
#do something else…  
end

def initialize  
@myVar1=0  
@myVar2=0  
@myVar3=0

```
   setVar(@myVar,1)
   setVar(@myVar,2)
   setVar(@myVar,3)

```

end  
end

test = MyClass.new

Beeing able to “send” the variable to setVar and have its value  
changed would save me many lines of code…

---

<div class="post-metadata">

**Author:** ![Filipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@Filipe](https://www.ruby-forum.com/u/Filipe)\
**Post date:** [November 24, 2007, 12:26am UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109/4 "2007-11-24T00:26:55Z")

</div>

Morton G. wrote:

> Also, Ruby can define  
> accessor methods for you:
> 
> class MyClass  
> attr\_accessor :myVar  
> def initialize  
> @myVar=0  
> self.myVar = 1  
> puts myVar # \<-- to\_s not needed; puts applies to\_s to objects  
> end  
> end  
> test = MyClass.new

Here it is explicitly:

class MyClass

def num=(val)  
@num = val  
end

def num  
@num  
end

def initialize(val)  
@num = val  
end

end

t = MyClass.new(1)  
puts t.num

which is equivalent to:

class MyClass  
attr\_accessor :num

def initialize(val)  
@num = val  
end

end

t = MyClass.new(1)  
puts t.num

---

<div class="post-metadata">

**Author:** ![Filipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@Filipe](https://www.ruby-forum.com/u/Filipe)\
**Post date:** [November 24, 2007, 2:13am UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109/5 "2007-11-24T02:13:10Z")

</div>

On Nov 23, 2007, at 6:24 PM, Filipe wrote:

> > > end  
> > > Unfortunately, this piece of code is returning 0 instead of the 1 I  
> > > @myVar=0  
> > > class MyClass  
> > > This will output 1 and may be what you are looking for.
> > 
> > Regards, Morton
> 
> The key point is that the setVar method is supposed to do a couple  
> more things than just set the value of the variable. I removed them  
> from the example just for the sake of simplicity.

That was a bad idea, since it made it impossible for anyone to  
understand your real problem and give you the help you were looking for.

> ```
> @myVar2=0
> 
> ```
> 
> Beeing able to “send” the variable to setVar and have its value  
> changed would save me many lines of code…

You can use Object#instance\_variable\_set to solve your problem by  
sending the variable’s name to setVar.

`
class MyClass
    def setVar(name, val)
       instance_variable_set(name, val)
       #do something else...
    end
```
def initialize
   @myVar1=0
   @myVar2=0
   @myVar3=0
   setVar(:@myVar1, 1)
   setVar(:@myVar2, 2)
   setVar(:@myVar3, 3)

```
end
end
MyClass.new.inspect # => “#<MyClass:0x24b58 @myVar3=3, @myVar2=2,
@myVar1=1>”
`

Regards, Morton

---

<div class="post-metadata">

**Author:** ![Filipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@Filipe](https://www.ruby-forum.com/u/Filipe)\
**Post date:** [November 25, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109/6 "2007-11-25T14:50:50Z")

</div>

> > > > end  
> > > > end  
> > > > test = MyClass.new  
> > > > Unfortunately, this piece of code is returning 0 instead of the 1 I  
> > > > would expect. Is there anything I could do?

The most stylish, Ruby way to do what you are trying to do is as  
follows:

class MyClass

def initialize  
@myVar1 = 0 # direct value manipulation  
self.myVar1 = 1 # sent through your wrapper  
puts @myVar1  
end

def myVar1=(value)  
@myVar1 = value  
# process extra steps here  
end

end

(Ehm, I should admit, there may be a better way to actually get the  
accessor method to fire inside of initialize than self.myVar1, but  
that’s what I found to work.)

---

<div class="post-metadata">

**Author:** ![Filipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@Filipe](https://www.ruby-forum.com/u/Filipe)\
**Post date:** [November 24, 2007, 12:37am UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109/7 "2007-11-24T00:37:52Z")

</div>

Filipe wrote:

> I have the following situation:  
> def initialize  
> @myVar=0  
> setVar(@myVar,1)\>
> 
> Unfortunately, this piece of code is returning 0 instead of the 1 I  
> would expect. Is there anything I could do?

Yes:

def initialize  
@myVar = 0  
@myVar = 1  
…

---

<div class="post-metadata">

**Author:** ![Filipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@Filipe](https://www.ruby-forum.com/u/Filipe)\
**Post date:** [November 25, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109/8 "2007-11-25T23:42:30Z")

</div>

On Nov 25, 11:50 am, Matt T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > > end  
> > > > > @myVar1 = 0 # direct value manipulation
> 
> (Ehm, I should admit, there may be a better way to actually get the  
> accessor method to fire inside of initialize than self.myVar1, but  
> that’s what I found to work.)

I would still have to writer a myVar=(value) method for every single  
variable. That’s what I’m trying to get rid of.

---

<div class="post-metadata">

**Author:** ![Filipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/35a633/32.png) [@Filipe](https://www.ruby-forum.com/u/Filipe)\
**Post date:** [November 26, 2007, 2:51am UTC](https://www.ruby-forum.com/t/class-variable-as-parameter/121109/9 "2007-11-26T02:51:48Z")

</div>

You can do something like

–  
class Foo

alias\_method :method\_missing2, :method\_missing;

def method\_missing(method, \*args, &block)  
if method.to\_s =~ /=$/  
set\_var($`, args.first);  
else  
method\_missing2(method, \*args, &block);  
end  
end

private  
def set\_var(name, value)  
instance\_variable\_set("@#{name}", value);  
#do stuff here…  
end

end

## foo = Foo.new; foo.car = “test”; p foo #=\> #\<Foo:0x2fb77e4 @car=“test”\>
