OO way of local variable passing

K

Krekna Mektek

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
 
J

Jan Svitok

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?

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.
 
R

Robert Klemme

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?

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
 
D

dblack

Hi --

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?

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,371
Latest member
TroyHursey

Latest Threads

Top