Newbie Question Passing variables to methods

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.

On 6/23/07, Starke [email protected] 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/

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/

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

Best of luck,
-greg

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

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}”

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. :slight_smile:

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs