Working with arrays

Hi, people I know some of ya might find me familiar but, I really am
still a novice in ruby thus i’ll need help once in awhile. I’d
defeinitely appreciate whatever help that is rendered! =)

ok, so back to our topic. I have a code where I would like to traverse
into files and folders and then at the same time pass in exceptions and
then after that select the whole load of files to carry out actions like
copying or deleting. But I’ll only need help for the traversing of
folders and the exceptions part. Here’s my code:

src1 = []
$source.each do |y|
Find.find(y + “/”) do |file|
src1 << file
$file_exception[i].each do |ex|
src1.delete_if {|x| /#{ex}/ =~ File.basename(file)}

end
  end

$source is an array of source paths like [“C:/Del”, “C:/My Pictures”]
$file_exception is an array of exceptions like [".txt", “.xls”]

The whole block of code is working fine except that it traverses into
the folders twice, and then parses all the file paths into src1(which is
not what is wanted) Is there any way to improve on my code?

I’m not an expert and I can’t tell you for sure what to do here, in my
own
bot I stored file contents in a variable name and then just rewrote the
file, appending whatever I needed to the end of the variables contents
and
then writing the variable to the file. Anyways, back to my reason for
responding, aren’t globals slow in Ruby? a friend of mine from
deviantART is
always nagging about not using global variables… lol. Anyone feel like
correcting him? And why are you passing in exceptions? :?


From: “Clement Ow” [email protected]
Sent: Monday, June 09, 2008 7:05 PM
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” [email protected]
Subject: Working with arrays

Hi –

On Tue, 10 Jun 2008, Clement Ow wrote:

Hi, people I know some of ya might find me familiar but, I really am
still a novice in ruby thus i’ll need help once in awhile. I’d
defeinitely appreciate whatever help that is rendered! =)

ok, so back to our topic. I have a code where I would like to traverse
into files and folders and then at the same time pass in exceptions and

I think you mean extensions, not exceptions.

then after that select the whole load of files to carry out actions like
copying or deleting. But I’ll only need help for the traversing of
folders and the exceptions part. Here’s my code:

src1 = []
$source.each do |y|
Find.find(y + “/”) do |file|
src1 << file
$file_exception[i].each do |ex|

What is i?

src1.delete_if {|x| /#{ex}/ =~ File.basename(file)}

end
end
$source is an array of source paths like [“C:/Del”, “C:/My Pictures”]
$file_exception is an array of exceptions like [".txt", “.xls”]

The whole block of code is working fine except that it traverses into
the folders twice, and then parses all the file paths into src1(which is
not what is wanted) Is there any way to improve on my code?

First, don’t use global variables. They don’t play nicely with
encapsulated code (i.e., code where knowledge and behavior is
encapsulated in objects which have to talk to each other, in an
orderly fashion, to get things done).

Second, I’d use File.extname rather than a pattern match on
File.basename. Something like this (semi-tested only):

source = %w{ C:/Del C:/My\ Pictures }
extensions = %w{ .txt .xls }
files = []

source.each do |s|
Find.find(s) do |file|
next if extensions.include?(File.extname(file))
files << file
end
end

David

2008/6/10 Clement Ow [email protected]:

ok, so back to our topic. I have a code where I would like to traverse
into files and folders and then at the same time pass in exceptions and
then after that select the whole load of files to carry out actions like
copying or deleting. But I’ll only need help for the traversing of
folders and the exceptions part. Here’s my code:

I pasted a solution to a this very question on May 30th (Subject “Find
files”). Didn’t you see it?
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/303503

src1 = []
$source.each do |y|
Find.find(y + “/”) do |file|

The slash is not needed.

src1 << file
$file_exception[i].each do |ex|
src1.delete_if {|x| /#{ex}/ =~ File.basename(file)}

This is extremely inefficient since you traverse src1 over and over
again. It would be better to test the current file against the
exceptions and only put it into src1 if it is not excluded.

end
end
$source is an array of source paths like [“C:/Del”, “C:/My Pictures”]
$file_exception is an array of exceptions like [“.txt”, “.xls”]

The whole block of code is working fine except that it traverses into
the folders twice, and then parses all the file paths into src1(which is
not what is wanted) Is there any way to improve on my code?

See above.

Cheers

robert

2008/6/11 Clement Ow [email protected]:

Robert wrote:

I pasted a solution to a this very question on May 30th (Subject “Find
files”). Didn’t you see it?
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/303503

Hi Robert, I did see your solution, just didnt have the time to reply…

You have time to ask questions but you do not have time to evaluate
the answers you get?

Btw it’s a sweet solution :wink: just that do u think it would be possible
to incoporate multiple source paths and each source paths has an array
of file_exceptions?

Yes. You just need to create a wrapper method around the one I posted.

Cheers

robert

AzimuthDragon wrote:

I’m not an expert and I can’t tell you for sure what to do here, in my
own
bot I stored file contents in a variable name and then just rewrote the
file, appending whatever I needed to the end of the variables contents
and
then writing the variable to the file. Anyways, back to my reason for
responding, aren’t globals slow in Ruby? a friend of mine from
deviantART is
always nagging about not using global variables… lol. Anyone feel like
correcting him? And why are you passing in exceptions? :?

I’m using globals because i have a config file that contains all the
path names and the different options that my program offers. I havent
heard abt global variables being slow. I think it’s still manageable on
my PC.(anyway, it is gonna be run in a server during production)

Exceptions are for files that I dun wanna select. For example, like i
want to select all files in C:/DEL other than say, the .xls files. I
wont wanna list down all the .xls files, hence the need for exceptions
(my company deals with tons of files) Exceptions is used because i also
want to exclude directory names as well.

Robert wrote:

I pasted a solution to a this very question on May 30th (Subject “Find
files”). Didn’t you see it?
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/303503

Hi Robert, I did see your solution, just didnt have the time to reply…
Btw it’s a sweet solution :wink: just that do u think it would be possible
to incoporate multiple source paths and each source paths has an array
of file_exceptions?

Thanks for all your concearns!

2008/6/11 Clement Ow [email protected]:

Btw it’s a sweet solution :wink: just that do u think it would be possible
to incoporate multiple source paths and each source paths has an array
of file_exceptions?

Yes. You just need to create a wrapper method around the one I posted.
What do you mean by wrapper method? care to explain? Thanks!

A “wrapper method” wraps the other method, i.e. it calls the other
method. You can forward the block from the outer method to the inner
method like this

def f(&b)
x(&b)
end

robert

Robert K. wrote:

2008/6/11 Clement Ow [email protected]:

Btw it’s a sweet solution :wink: just that do u think it would be possible
to incoporate multiple source paths and each source paths has an array
of file_exceptions?

Yes. You just need to create a wrapper method around the one I posted.
What do you mean by wrapper method? care to explain? Thanks!

A “wrapper method” wraps the other method, i.e. it calls the other
method. You can forward the block from the outer method to the inner
method like this

def f(&b)
x(&b)
end

robert

So what you are saying is I create a method to call another method which
is already defined? But won’t it be easier to directly call the method?
Sorry, I’m abit confused here…

Robert K. wrote:

2008/6/11 Clement Ow [email protected]:

Robert wrote:

I pasted a solution to a this very question on May 30th (Subject “Find
files”). Didn’t you see it?
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/303503

Hi Robert, I did see your solution, just didnt have the time to reply…

You have time to ask questions but you do not have time to evaluate
the answers you get?
I think i didnt explain myslef properly, I was trying out your solution,
trying to incoporate multiple source paths + file exceptions, and in the
meantime i wanted to source for other alternatives hence thhis post :wink:

Btw it’s a sweet solution :wink: just that do u think it would be possible
to incoporate multiple source paths and each source paths has an array
of file_exceptions?

Yes. You just need to create a wrapper method around the one I posted.
What do you mean by wrapper method? care to explain? Thanks!

Clement Ow wrote:

Robert K. wrote:

2008/6/11 Clement Ow [email protected]:

Btw it’s a sweet solution :wink: just that do u think it would be possible
to incoporate multiple source paths and each source paths has an array
of file_exceptions?

Yes. You just need to create a wrapper method around the one I posted.
What do you mean by wrapper method? care to explain? Thanks!

A “wrapper method” wraps the other method, i.e. it calls the other
method. You can forward the block from the outer method to the inner
method like this

def f(&b)
x(&b)
end

robert

PS: Oops, i hit submit accidentally without finishing my post
So what you are saying is I create a method to call another method which
is already defined? But won’t it be easier to directly call the method?
Sorry, I’m abit confused here… So how is it by creating a wrapper
method, I would be able to pass in multiple source and dest paths, and
exclusions?

Regards,
Clement

2008/6/12 Clement Ow [email protected]:

A “wrapper method” wraps the other method, i.e. it calls the other
So what you are saying is I create a method to call another method which
is already defined? But won’t it be easier to directly call the method?
Sorry, I’m abit confused here… So how is it by creating a wrapper
method, I would be able to pass in multiple source and dest paths, and
exclusions?

Separation of concerns. The innder method (the version I provided)
does the traversing and the exclusion filtering and the outer method
uses the inner method to work with multiple source directories and
exclusions.

robert