# Beginner's question about passing arrays as arguments to met

**URL:** <https://www.ruby-forum.com/t/beginners-question-about-passing-arrays-as-arguments-to-met/75779>\
**Category:** Ruby\
**Created:** [November 13, 2006, 11:36am UTC](https://www.ruby-forum.com/t/beginners-question-about-passing-arrays-as-arguments-to-met/75779 "2006-11-13T11:36:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 13, 2006, 11:36am UTC](https://www.ruby-forum.com/t/beginners-question-about-passing-arrays-as-arguments-to-met/75779/1 "2006-11-13T11:36:30Z")

</div>

I am doing a little script that builds an array (called darray) of all  
filenames in a directory tree. I am doing this by recursively calling  
a method (called enter\_dir) and passing the partially filled array  
(called darray) down through the recursive calls.

The code works, but I am concerned about the efficiency of passing what  
is potentially quite a large array around so many times. Should I be  
concerned about this or not?

I also wrote the code using a global array ($darray) and this worked  
too and didn’t need to array to be passed down through the recursions.

What are the pros and cons of these two approaches?

def enter\_dir(this\_dir, darray)  
farray = []  
this\_dir.each {|fname|  
farray \<\< fname  
}  
farray.each {|fname|  
file\_path = this\_dir.path + ‘/’ + fname  
if File.stat(file\_path).directory?  
this\_dir\_sub = Dir.new(file\_path)  
if fname != ‘.’ && fname != ‘…’  
enter\_dir(this\_dir\_sub, darray)  
end  
else  
darray \<\< file\_path  
end  
}  
end

this\_dir = Dir.new(’/home/tony/Ruby/dot\_recycle’)  
darray = []  
enter\_dir(this\_dir, darray)  
puts darray

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 13, 2006, 11:44am UTC](https://www.ruby-forum.com/t/beginners-question-about-passing-arrays-as-arguments-to-met/75779/2 "2006-11-13T11:44:33Z")

</div>

On 13/11/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> too and didn’t need to array to be passed down through the recursions.  
> file\_path = this\_dir.path + ‘/’ + fname
> 
> this\_dir = Dir.new(‘/home/tony/Ruby/dot\_recycle’)  
> darray = []  
> enter\_dir(this\_dir, darray)  
> puts darray

You’re not actually passing the array around whenever you call  
enter\_dir, rather you’re passing a reference to the object, so the  
performance penalties should not be noticable at all.

Farrel

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 13, 2006, 2:48pm UTC](https://www.ruby-forum.com/t/beginners-question-about-passing-arrays-as-arguments-to-met/75779/3 "2006-11-13T14:48:50Z")

</div>

Farrel L. \<farrel.lifson [gmail.com](http://gmail.com)\> writes:

> You’re not actually passing the array around whenever you call  
> enter\_dir, rather you’re passing a reference to the object, so the  
> performance penalties should not be noticable at all.
> 
> Farrel

Hopefully that makes sense, but if it doesn’t, imagine tying a long  
piece of  
string to a large rock and then giving the other end of the string to  
someone to  
hold

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 13, 2006, 3:57pm UTC](https://www.ruby-forum.com/t/beginners-question-about-passing-arrays-as-arguments-to-met/75779/4 "2006-11-13T15:57:03Z")

</div>

On 11/13/06, Gareth A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hopefully that makes sense, but if it doesn’t, imagine tying a long piece of  
> string to a large rock and then giving the other end of the string to someone to  
> hold

Or there’s always the Binky Pointer video:

> **[Binky Pointer Fun Video](http://cslibrary.stanford.edu/104/)**
>
> Stanford CS Education Library: a fun 3minute video that explains the basics features of pointers.

-Harold
