addis_a
1
I just found a way to check if a dir is empty or not.
Dir.chdir ‘someDir’
is_empty = Dir.glob’*’
if is_empty == true
#do something
elsif is_empty != true
#do something
else
end
Is this a good way to check?
I know there are better ways but if this is reliable why not?
cynic limbu wrote in post #1162237:
Dir.chdir ‘someDir’
is_empty = Dir.glob’*’
if is_empty == true
#do something
elsif is_empty != true
#do something
else
end
What does it do? Are you happy with this code?
(from ruby core)
Dir.glob( pattern, [flags] ) -> matches
Dir.glob( pattern, [flags] ) { |filename| block } -> nil
Expands pattern, which is an Array of patterns or a pattern String, and
returns the results as matches or as arguments given to the block.
irb(main):002:0> Dir.entries(’/tmp/edir’)
=> ["…", “.”]
irb(main):003:0> Dir.glob(’/tmp/edir’)
=> ["/tmp/edir"]
irb(main):004:0> Dir.chdir ‘/tmp/edir’
=> 0
irb(main):005:0> Dir.glob(’’)
=> []
irb(main):006:0> Dir.glob(’’).class
=> Array
Under which circumstances, your ‘is_empty?’ is false or nil?
Michael U. wrote in post #1162352:
What does it do? Are you happy with this code?
I just realized I’m a fool. I thought this actually worked.
It turns out it will just return false
whether the directory is empty or not.
Under which circumstances, your ‘is_empty?’ is false or nil?
Sorry.