beginner's question about passing arrays as arguments to methods

Discussion in 'Ruby' started by gigaday@googlemail.com, Nov 13, 2006.

  1. Guest

    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
     
    , Nov 13, 2006
    #1
    1. Advertising

  2. On 13/11/06, <> wrote:
    > 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


    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
     
    Farrel Lifson, Nov 13, 2006
    #2
    1. Advertising

  3. On 11/13/06, Gareth Adams <> wrote:
    > Farrel Lifson <farrel.lifson <at> 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
    >
    >
    >


    Or there's always the Binky Pointer video:
    http://cslibrary.stanford.edu/104/

    -Harold
     
    Harold Hausman, Nov 13, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    603
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Morgan

    Again on passing arrays as arguments

    Morgan, Feb 8, 2004, in forum: C Programming
    Replies:
    2
    Views:
    406
    Jumbo
    Feb 8, 2004
  3. Philipp
    Replies:
    21
    Views:
    1,140
    Philipp
    Jan 20, 2009
  4. Kenneth McDonald
    Replies:
    5
    Views:
    329
    Kenneth McDonald
    Sep 26, 2008
  5. oldyork90
    Replies:
    10
    Views:
    348
    Jorge
    Sep 27, 2008
Loading...

Share This Page