[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Working with arrays

Clement Ow

6/10/2008 2:06:00 AM

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?
--
Posted via http://www.ruby-....

10 Answers

ProgramDragon

6/10/2008 4:11:00 AM

0

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" <clement.ow@asia.bnpparibas.com>
Sent: Monday, June 09, 2008 7:05 PM
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Subject: 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?
> --
> Posted via http://www.ruby-....
>
>

David A. Black

6/10/2008 5:31:00 AM

0

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

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.r... for details and updates!

Robert Klemme

6/10/2008 1:36:00 PM

0

2008/6/10 Clement Ow <clement.ow@asia.bnpparibas.com>:
> 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-t...

> 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

--
use.inject do |as, often| as.you_can - without end

Clement Ow

6/11/2008 2:00:00 AM

0

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

Hi Robert, I did see your solution, just didnt have the time to reply..
Btw it's a sweet solution ;) 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!

--
Posted via http://www.ruby-....

Robert Klemme

6/11/2008 6:53:00 AM

0

2008/6/11 Clement Ow <clement.ow@asia.bnpparibas.com>:
> 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-t...
>
> 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 ;) 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

--
use.inject do |as, often| as.you_can - without end

Clement Ow

6/11/2008 8:18:00 AM

0

Robert Klemme wrote:
> 2008/6/11 Clement Ow <clement.ow@asia.bnpparibas.com>:
>> 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-t...
>>
>> 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 ;)
>
>> Btw it's a sweet solution ;) 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!
--
Posted via http://www.ruby-....

Robert Klemme

6/11/2008 11:32:00 AM

0

2008/6/11 Clement Ow <clement.ow@asia.bnpparibas.com>:
>>> Btw it's a sweet solution ;) 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


--
use.inject do |as, often| as.you_can - without end

Clement Ow

6/12/2008 2:54:00 AM

0

Robert Klemme wrote:
> 2008/6/11 Clement Ow <clement.ow@asia.bnpparibas.com>:
>>>> Btw it's a sweet solution ;) 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..
--
Posted via http://www.ruby-....

Clement Ow

6/12/2008 2:56:00 AM

0

Clement Ow wrote:
> Robert Klemme wrote:
>> 2008/6/11 Clement Ow <clement.ow@asia.bnpparibas.com>:
>>>>> Btw it's a sweet solution ;) 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

--
Posted via http://www.ruby-....

Robert Klemme

6/12/2008 7:10:00 AM

0

2008/6/12 Clement Ow <clement.ow@asia.bnpparibas.com>:
> Clement Ow wrote:
>> Robert Klemme wrote:
>>> 2008/6/11 Clement Ow <clement.ow@asia.bnpparibas.com>:
>>>>>> Btw it's a sweet solution ;) 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?

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


--
use.inject do |as, often| as.you_can - without end