[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Find.find --- returns directories/files backwards

Joseph Schiller

3/10/2007 3:55:00 PM

Hi,Try something like thisd = Dir.open('/user/path/Documents')d.entries.sort=> [ ".", "..", "doc1", "doc2", etc ]for some reason block processing is counter intuitive herethis code does not worke = Dir.open('/user/path/Documents')e.entries.sort {|file| puts file }=> warning: multiple values for a block parameter (2 for 1)Regards,joe----- Original Message ----From: Brad <bradaskins@gmail.com>To: ruby-talk ML <ruby-talk@ruby-lang.org>Sent: Friday, March 9, 2007 7:35:05 PMSubject: Find.find --- returns directories/files backwardsNew user question:It seems to me when I run:Find.find('/user/name/documents') {|path| puts path}it returns all the directories in the reverse order. I was expectingthe directories to be returned in alphabetical order, but that doesn'tseem to be the case. Also, in one case it reads half of a directory'sfiles, then the sub dirs and then it finished reading the rest of thedirectory it started in and finished writing them in the Putsstatement.Am I missing something? How do you do get it to write out thedirectories in alphabetical order?Any and all help welcome.Thank you.Brad ____________________________________________________________________________________It's here! Your new message! Get new email alerts with the free Yahoo! Toolbar.http://tools.search.yahoo.com/toolbar/feat...

2 Answers

Sebastian Hungerecker

3/10/2007 4:08:00 PM

0

Joseph Schiller wrote:
> for some reason block processing is counter intuitive here
> this code does not work
>
> e = Dir.open('/user/path/Documents')
> e.entries.sort {|file| puts file }
> => warning: multiple values for a block parameter (2 for 1)

It doesn't work because you pass a block that accepts only one argument to
sort. Any block passed to sort is supposed to take two arguments, compare
these arguments and return -1, 0 or 1 depending on which argument is greater.

What you apparenty want to do would be archieved by writing
e.entries.sort.each {|file| puts file }
thus passing the block to each and not to sort.


--
Ist so, weil ist so
Bleibt so, weil war so

Robert Klemme

3/10/2007 4:28:00 PM

0

On 10.03.2007 17:07, Sebastian Hungerecker wrote:
> Joseph Schiller wrote:
>> for some reason block processing is counter intuitive here
>> this code does not work
>>
>> e = Dir.open('/user/path/Documents')
>> e.entries.sort {|file| puts file }
>> => warning: multiple values for a block parameter (2 for 1)
>
> It doesn't work because you pass a block that accepts only one argument to
> sort. Any block passed to sort is supposed to take two arguments, compare
> these arguments and return -1, 0 or 1 depending on which argument is greater.
>
> What you apparenty want to do would be archieved by writing
> e.entries.sort.each {|file| puts file }
> thus passing the block to each and not to sort.

There is also a class method entries, so the above code can be rewritten as

Dir.entries('/user/path/Documents').sort.each {|e| puts e}

or even shorter

puts Dir.entries('/user/path/Documents').sort

Kind regards

robert