# Newbie Question Passing variables to methods

**URL:** <https://www.ruby-forum.com/t/newbie-question-passing-variables-to-methods/107616>\
**Category:** Ruby\
**Created:** [June 24, 2007, 4:46am UTC](https://www.ruby-forum.com/t/newbie-question-passing-variables-to-methods/107616 "2007-06-24T04:46:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Starke](https://www.ruby-forum.com/u/Starke)\
**Post date:** [June 24, 2007, 4:46am UTC](https://www.ruby-forum.com/t/newbie-question-passing-variables-to-methods/107616/1 "2007-06-24T04:46:20Z")

</div>

Im trying to set up a method and pass a varible to it then out put the  
new manipulated varible back out. Here is what I have

* * *

var1 = ‘test’

def change var\_in  
puts var\_in  
var\_in = ‘test1’  
puts var\_in  
end

change var1

* * *

As you can see, I pass in var1 into the method and then I change the  
value to ‘test1’. I would like to method to go back and update var1,  
so that if passed into another method it reflects the change that  
method change var\_in did. Does this make since? Im new to  
programming.

---

<div class="post-metadata">

**Author:** ![Starke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Starke](https://www.ruby-forum.com/u/Starke)\
**Post date:** [June 24, 2007, 5:39am UTC](https://www.ruby-forum.com/t/newbie-question-passing-variables-to-methods/107616/2 "2007-06-24T05:39:22Z")

</div>

On 6/23/07, Starke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> puts var\_in  
> programming.  
> Hi. What you are talking about are instance variables. Let’s move  
> your print statements outside of your method:

#—  
@var1 = “test”

puts @var1

def change  
@var1 = “test1”  
end

change

puts @var1  
#—

or, if you want to pass the value into your method:

#—  
def change(value)  
@var1 = value  
end

change ‘something’  
puts @var1  
#—

You’ll want to pick up a basic understanding of scoping, data  
encapsulation, and object oriented design. You might start with some  
simple tutorials that help you learn by example, such as TryRuby:  
[http://tryruby.hobix.com/](http://tryruby.hobix.com/)

The first edition of Programming Ruby is also available free online,  
and is a good book. Once you get somewhat familiar, you might want to  
pick up the second edition as a reference

[http://www.rubycentral.com/book/](http://www.rubycentral.com/book/)

And of course, if you run into problems, post here. 🙂

Best of luck,  
-greg

---

<div class="post-metadata">

**Author:** ![Starke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Starke](https://www.ruby-forum.com/u/Starke)\
**Post date:** [June 24, 2007, 6:50am UTC](https://www.ruby-forum.com/t/newbie-question-passing-variables-to-methods/107616/3 "2007-06-24T06:50:26Z")

</div>

On Jun 23, 2007, at 10:45 PM, Starke wrote:

> puts var\_in  
> programming.  
> A simple approach is to add one line of code at the end of your  
> ‘change’ method, so the new value is returned, and then assign ‘var1’  
> to that value.

var1 = ‘test’

def change var\_in  
puts var\_in  
var\_in = ‘test1’  
puts var\_in  
var\_in # Ruby returns the value of last expression evaluated in a  
method  
end

var1 = change var1 # this is a common Ruby idiom for updating a variable  
var1 # =\> “test1”

Because the value you are returning is independent of the argument  
‘var\_in’, if the ‘puts’ weren’t there, the whole thing could be  
reduced to:

var1 = ‘test’

def change  
‘test1’  
end

var1 = change  
var1 # =\> “test1”

But that’s too trivial, so let’s look at a version where ‘change’  
depends on its argument:

var1 = ‘test’

def change var\_in  
“#{var\_in} contains #{var\_in.size} characters”  
end

var1 = change var1  
var1 # =\> “test contains 4 characters”

Regards, Morton

---

<div class="post-metadata">

**Author:** ![Starke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Starke](https://www.ruby-forum.com/u/Starke)\
**Post date:** [June 24, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/newbie-question-passing-variables-to-methods/107616/4 "2007-06-24T19:07:28Z")

</div>

Starke wrote:

> As you can see, I pass in var1 into the method and then I change the  
> value to ‘test1’. I would like to method to go back and update var1,  
> so that if passed into another method it reflects the change that  
> method change var\_in did. Does this make since? Im new to  
> programming.

What you want to do is a “call by reference”.  
What you get is a “call by value”.  
This is just to let you know the terminology.  
Search the web for more about these concepts.

As soon as you assign a new value (object) to  
your local variable in the method change(), it  
will no longer point to the object passed as  
argument, but instead point to a new object.  
The object that was passed remains unchanged.

The code below my signature could make a lot  
of things clearer. Just run it and come back  
if you have questions.

Btw: You don’t need to know all this to use  
Ruby. So don’t worry and don’t forget to have  
fun with this wonderful and easy programming  
language.

HM

def change x  
puts “With value just passed:”,  
“ID of x: #{x.object\_id}”,  
“Value of x: #{x}”  
x = ‘abc’  
puts "With new value: ",  
“ID of x: #{x.object\_id}”,  
“Value of x: #{x}”  
return x  
end

a = “Hallo”  
puts “Before calling change():”,  
“ID of a: #{a.object\_id}”  
rv = change a # return value  
puts “After calling change():”,  
“ID of rv: #{rv.object\_id}”,  
“Value of rv: #{rv}”,  
“ID of a: #{a.object\_id}”,  
“Value of a: #{a}”

---

<div class="post-metadata">

**Author:** ![Starke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@Starke](https://www.ruby-forum.com/u/Starke)\
**Post date:** [June 24, 2007, 10:55am UTC](https://www.ruby-forum.com/t/newbie-question-passing-variables-to-methods/107616/5 "2007-06-24T10:55:36Z")

</div>

On 24.06.2007 04:41, Starke wrote:

> puts var\_in  
> end
> 
> change var1
> 
> * * *
> 
> As you can see, I pass in var1 into the method and then I change the  
> value to ‘test1’.

No. You pass the value of “var1” in, i.e. the object which “var1”  
refers to. In passing in that value it is assigned to “var\_in”. Now  
you have two variables referring the same object. Then you make  
“var\_in” point to another object (your string ‘test1’). But “var1” in  
the other scope still points to the original value / object.

> I would like to method to go back and update var1,  
> so that if passed into another method it reflects the change that  
> method change var\_in did. Does this make since? Im new to  
> programming.

It may make sense and you can actually do such things in other  
programming languages. I have never felt the need to do this in Ruby so  
you definitively can live without this feature. There are a number of  
options you have, two of them have been mentioned in this thread  
(assignment of the return value, instance variables). Which of those is  
the most appropriate depends on the problem you are trying to solve. At  
the moment this looks like a solution looking for a problem. 🙂

Kind regards

robert
