# OO way of local variable passing

**URL:** <https://www.ruby-forum.com/t/oo-way-of-local-variable-passing/81359>\
**Category:** Ruby\
**Created:** [January 8, 2007, 10:10am UTC](https://www.ruby-forum.com/t/oo-way-of-local-variable-passing/81359 "2007-01-08T10:10:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 8, 2007, 10:10am UTC](https://www.ruby-forum.com/t/oo-way-of-local-variable-passing/81359/1 "2007-01-08T10:10:02Z")

</div>

Hi,

What is the OO way to do this?

I’ve got a front-end script:

front.rb which calls site.rb

in site.rb there is a method1 and method2.

method1 creates a directory (the name is based on some other info) and  
this directoryname is needed in method2.

in front.rb I do this:

I create the site object and then call:  
site.method1 (new dir is created)  
site.method2 (do some things in this directory)

So, the first method is like:

def method1

…  
new\_dir = […magic code…]  
…  
end

def method2  
…  
get\_files = @basedir + new\_dir  
…  
end

But, this new\_dir variable is not known in method2.

- Do I need to return this new\_dir variable from method1, and call the  
other method with new\_dir as argument like site.method2(new\_dir) ?

- Do I have to make from new\_dir a class variable? (the way I have it  
now)

- Is there another way to do this?

Thanx!

Krekna

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 8, 2007, 10:38am UTC](https://www.ruby-forum.com/t/oo-way-of-local-variable-passing/81359/2 "2007-01-08T10:38:04Z")

</div>

On 1/8/07, Krekna M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def method2
> 
> - Do I have to make from new\_dir a class variable? (the way I have it now)
> 
> - Is there another way to do this?

I’d say, the first two options are most usual. If you don’t need the  
var anywhere else, I’d use the first option. In other words, it  
depends on the relationship of site object to the created directory.  
If they are close enough, you can make it an instance variable  
(@new\_dir). Other possibility is to create a helper class SiteDir with  
methods create and get\_files, and @new\_dir.

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 8, 2007, 11:06am UTC](https://www.ruby-forum.com/t/oo-way-of-local-variable-passing/81359/3 "2007-01-08T11:06:10Z")

</div>

On 08.01.2007 10:09, Krekna M. wrote:

> def method2
> 
> - Do I have to make from new\_dir a class variable? (the way I have it now)
> 
> - Is there another way to do this?

It seems to me that method1 and method2 are more like functions (i.e.  
they are defined on top level). In that case I’d pass the directory  
name as argument to method2. It’s not completely clear to me, what  
“magic code” does, but assuming that it generates the name then new\_dir  
should definitively be a return value from method1.

Another option is to refactor the code to create an additional function  
magic\_code that does nothing else than creating the directory name which  
then is passed to method1 and method2.

There are other options like creating a helper class that contains all  
these methods and has a member for the directory name. Then all methods  
can access this name.

For more specific advice we would probably have to see the code or at  
least get more information about the problem you are trying to solve.  
My general advice is to create methods (and classes) that do one thing  
and one thing only, i.e. proper modularize code. Your directory name  
generation code seems to be a candidate for factoring out into another  
method.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 8, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/oo-way-of-local-variable-passing/81359/4 "2007-01-08T13:48:31Z")

</div>

Hi –

On Mon, 8 Jan 2007, Krekna M. wrote:

> def method2
> 
> - Do I have to make from new\_dir a class variable? (the way I have it now)
> 
> - Is there another way to do this?

If I’m understanding the situation correctly it looks like you could  
use an instance variable:

def method1  
…  
@new\_dir = (whatever)  
…  
end

def method2  
…  
get\_files = @basedir + @new\_dir  
…  
end

That’s the normal way of maintaining state for a particular object.

David

---

<div class="post-metadata">

**Author:** ![Krekna\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a9adbd/32.png) [@Krekna\_M](https://www.ruby-forum.com/u/Krekna_M)\
**Post date:** [January 8, 2007, 7:27pm UTC](https://www.ruby-forum.com/t/oo-way-of-local-variable-passing/81359/5 "2007-01-08T19:27:29Z")

</div>

you said you had it as a class variable now but you’ve written it as a  
local variable.

a class variable would be @@new\_dir and available to all instances of  
that class.

Or maybe I read your mail wrong 😉

Krekna M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Hi,

What is the OO way to do this?

I’ve got a front-end script:

front.rb which calls site.rb

in site.rb there is a method1 and method2.

method1 creates a directory (the name is based on some other info) and  
this directoryname is needed in method2.

in front.rb I do this:

I create the site object and then call:  
site.method1 (new dir is created)  
site.method2 (do some things in this directory)

So, the first method is like:

def method1

…  
new\_dir = […magic code…]  
…  
end

def method2  
…  
get\_files = @basedir + new\_dir  
…  
end

But, this new\_dir variable is not known in method2.

- Do I need to return this new\_dir variable from method1, and call the  
other method with new\_dir as argument like site.method2(new\_dir) ?

- Do I have to make from new\_dir a class variable? (the way I have it  
now)

- Is there another way to do this?

Thanx!

Krekna

Send instant messages to your online friends

> **[Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](https://www.yahoo.com/)**
>
> Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!
