[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Non Existent Method Dir.empty? Description via "ri"

Wolfgang Nádasi-donner

8/12/2007 9:01:00 PM

Moin, moin!

There is a description for a method that does not exist presented by
"ri":

>>>>> Console Protocol >>>>>
C:\Dokumente und Einstellungen\wolfgang>ri Dir.empty?
------------------------------------------------------------ Dir::empty?
Dir::empty?(path)
------------------------------------------------------------------------
Returns whether or not +path+ is empty. Returns false if +path+ is
not a directory, or contains any files other than '.' or '..'.



C:\Dokumente und Einstellungen\wolfgang>irb
irb(main):001:0> Dir::empty?('./')
NoMethodError: undefined method `empty?' for Dir:Class
from (irb):1
irb(main):002:0>
>>>>> EOCP >>>>>

Produced via OneClickInstaller 186-25 - ruby 1.8.6 (2007-03-13
patchlevel 0) [i386-mswin32]

Wolfgang Nádasi-Donner
--
Posted via http://www.ruby-....

23 Answers

Tim Hunter

8/12/2007 9:08:00 PM

0

Interesting. This is from Ruby 1.8.6 on OS X. I've got a pretty vanilla
install, no gems.

timothyhunter$ ri Dir::empty?
Nothing known about Dir::empty?
timothyhunter$ ruby -e "p RUBY_VERSION"
"1.8.6"

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


John Joyce

8/14/2007 12:58:00 PM

0

I've got Ruby 1.8.4 on OSX with many gems installed.
When I do
ri empty

I get this:

More than one method matched your request. You can refine
your search by asking for information on one of:

Array#empty?, Hash#empty?, Queue#empty?, Set#empty?,
String#empty?,
StringScanner#empty?, ThreadsWait#empty?

Looks like you've confused a method from another class.

You might want to use something like this:

def dir_empty?(path_to_dir)
Dir.chdir(path_to_dir)
if Dir.glob('*').length > 0
return false
else
return true
end



Bertram Scharpf

8/14/2007 1:09:00 PM

0

Hi,

Am Dienstag, 14. Aug 2007, 21:58:17 +0900 schrieb John Joyce:
> You might want to use something like this:
>
> def dir_empty?(path_to_dir)
> Dir.chdir(path_to_dir)
> if Dir.glob('*').length > 0
> return false
> else
> return true
> end

What's this?

1. 'end' is missing.
2. 'if ... then false else true end' is a lot of hot air
around 'not ...'.
3. As a side effect the method changes the current
directory.

Say

def dir_empty? path_to_dir
Dir.chdir path_to_dir do
not Dir.glob('*').length > 0
end
end

Then, you may consider hidden files ...

First think, then write. Sorry!

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Chris Hulan

8/14/2007 1:38:00 PM

0

On Aug 14, 9:08 am, Bertram Scharpf <li...@bertram-scharpf.de> wrote:
snip
> First think, then write. Sorry!
>
> Bertram
>

A little more thought and a review of Ruby-doc shows that it could be
as simple as:
Class Dir
def empty?(path=Dir.pwd)
Dir.entries(path).empty?
end
end

cheers
Chris

David A. Black

8/14/2007 1:41:00 PM

0

Bertram Scharpf

8/14/2007 2:24:00 PM

0

Hi,

Am Dienstag, 14. Aug 2007, 22:40:58 +0900 schrieb dblack@rubypal.com:
> On Tue, 14 Aug 2007, Bertram Scharpf wrote:
>> First think, then write. Sorry!
>
> Please leave the snideness and nastiness out of it. They add nothing
> but snideness and nastiness.

I didn't mean it as harmful as it obviously sounds. Sorry,
again.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

John Joyce

8/14/2007 3:15:00 PM

0


On Aug 14, 2007, at 9:24 AM, Bertram Scharpf wrote:

> Hi,
>
> Am Dienstag, 14. Aug 2007, 22:40:58 +0900 schrieb dblack@rubypal.com:
>> On Tue, 14 Aug 2007, Bertram Scharpf wrote:
>>> First think, then write. Sorry!
>>
>> Please leave the snideness and nastiness out of it. They add nothing
>> but snideness and nastiness.
>
> I didn't mean it as harmful as it obviously sounds. Sorry,
> again.
Then take your own advice about thinking first and then writing, friend.
No offense taken, I know I'm not the only one who writes something
and posts quickly because there are other things to do.
I wasn't trying to give a 100% perfect solution, just a quick example
idea of what could be done.
Lack of the end token would be caught by the interpreter and the OP
would notice or learn from it anyway.
everybody forgets an end here and there.
As for hidden files, well that's somebody else's problem, isn't it?
Depending on point of view, a directory only containing hidden files
could be considered empty!
So whether or not those files are important depends on the program's
needs.
In that case, hidden or not should be an optional argument with a
default.

That said, though we really have to ask what is meant by empty?
Directories on many platforms contain hidden files or hidden
symlinks. I think that calls for more than one method definition to
handle those respective distinctions.
hidden_files_in_dir?(path_to_dir)
symlinks_in_dir?(path_to_dir)

As is often the case, d.a.b. has the most concise solution...

Daniel Berger

8/14/2007 4:05:00 PM

0



On Aug 12, 3:01 pm, "Wolfgang Nádasi-donner" <ed.oda...@wonado.de>
wrote:
> Moin, moin!
>
> There is a description for a method that does not exist presented by
> "ri":
>
> >>>>> Console Protocol >>>>>
>
> C:\Dokumente und Einstellungen\wolfgang>ri Dir.empty?
> ------------------------------------------------------------ Dir::empty?
> Dir::empty?(path)
> ------------------------------------------------------------------------
> Returns whether or not +path+ is empty. Returns false if +path+ is
> not a directory, or contains any files other than '.' or '..'.
>
> C:\Dokumente und Einstellungen\wolfgang>irb
> irb(main):001:0> Dir::empty?('./')
> NoMethodError: undefined method `empty?' for Dir:Class
> from (irb):1
> irb(main):002:0>

You're picking up Dir.empty? from win32-dir, which ships with the one-
click installer. In order to use it, you have to require it first. :)

Regards,

Dan



Bertram Scharpf

8/14/2007 4:17:00 PM

0

Hi,

Am Mittwoch, 15. Aug 2007, 00:15:25 +0900 schrieb John Joyce:
>
> On Aug 14, 2007, at 9:24 AM, Bertram Scharpf wrote:
>> Am Dienstag, 14. Aug 2007, 22:40:58 +0900 schrieb dblack@rubypal.com:
>>> On Tue, 14 Aug 2007, Bertram Scharpf wrote:
>>>> First think, then write. Sorry!
>>>
>>> Please leave the snideness and nastiness out of it. They add nothing
>>> but snideness and nastiness.
>>
>> I didn't mean it as harmful as it obviously sounds. Sorry,
>> again.
> Then take your own advice about thinking first and then writing, friend.

I was really hurt by your code.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

John Joyce

8/14/2007 4:35:00 PM

0


>
> You're picking up Dir.empty? from win32-dir, which ships with the one-
> click installer. In order to use it, you have to require it first. :)
>
> Regards,
>
> Dan
>
>
>
So it kind of exists?
Oh no, platform dependent Ruby....