[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Sth. wrong with File.file? (or with me

Michael Weller

10/7/2004 4:31:00 PM

Hi!
I wrote this code to walk a file tree from a given starting directory:

def each_dir(start_dir, &action)
files = Dir.new(start_dir).to_a[2..-1] #remove . and ..
files.each do |f|
unless File.file? f then
yield f
each_dir(start_dir << "\\" << f, &action)
end
end
end

But this doesn't work properly because it seems like File.file?
sometimes errs...
I know RTFS ;-) but I couldn't find it :-((
Can someone guide me?

Michael


93 Answers

ts

10/7/2004 4:45:00 PM

0

>>>>> "M" == Michael Weller <michael@gutschi.de> writes:

M> files = Dir.new(start_dir).to_a[2..-1] #remove . and ..
M> files.each do |f|
M> unless File.file? f then

there is only the filename in `f'

svg% ls yyy
aaa
svg%

svg% ruby -e 'Dir.new("yyy").each {|f| p f}'
"."
".."
"aaa"
svg%

you must add `start_dir' for the test File::file?

you can also look at Find::find

svg% ruby -rfind -e 'Find.find("yyy") {|f| p f}'
"yyy"
"yyy/aaa"
svg%


Guy Decoux


Michael Weller

10/7/2004 4:56:00 PM

0

ts wrote:
>>>>>>"M" == Michael Weller <michael@gutschi.de> writes:
>
>
> M> files = Dir.new(start_dir).to_a[2..-1] #remove . and ..
> M> files.each do |f|
> M> unless File.file? f then
>
> there is only the filename in `f'
>

Duh. I was almost sure it was me :-))

> svg% ls yyy
> aaa
> svg%
>
> svg% ruby -e 'Dir.new("yyy").each {|f| p f}'
> "."
> ".."
> "aaa"
> svg%
>
> you must add `start_dir' for the test File::file?
>
> you can also look at Find::find
>
> svg% ruby -rfind -e 'Find.find("yyy") {|f| p f}'
> "yyy"
> "yyy/aaa"
> svg%
>

Thanks for your help!
Yes,I think Find was what I was initially searching for. Sth. like
Find::prune would have been my next task...

Michael

>
> Guy Decoux
>
>


Mohammad Khan

10/7/2004 5:00:00 PM

0

On Thu, 2004-10-07 at 12:30, Michael Weller wrote:
> Hi!
> I wrote this code to walk a file tree from a given starting directory:
>
> def each_dir(start_dir, &action)
puts "start_dir: #{start_dir}"

> files = Dir.new(start_dir).to_a[2..-1] #remove . and ..
# Above line will throw an exception
#if start_dir is not a Directory

> files.each do |f|
> unless File.file? f then
> yield f
> each_dir(start_dir << "\\" << f, &action)
> end
> end
> end
>
> But this doesn't work properly because it seems like File.file?
> sometimes errs...
> I know RTFS ;-) but I couldn't find it :-((
> Can someone guide me?
>
> Michael

RTFS ... lol

--
Mohammad Khan <mkhan@lextranet.com>
Legal Computer Solutions, Inc.



Robert Klemme

10/8/2004 9:18:00 AM

0


"Michael Weller" <michael@gutschi.de> schrieb im Newsbeitrag
news:41657516.4000602@gutschi.de...
> ts wrote:
> >>>>>>"M" == Michael Weller <michael@gutschi.de> writes:
> >
> >
> > M> files = Dir.new(start_dir).to_a[2..-1] #remove . and ..
> > M> files.each do |f|
> > M> unless File.file? f then
> >
> > there is only the filename in `f'
> >
>
> Duh. I was almost sure it was me :-))
>
> > svg% ls yyy
> > aaa
> > svg%
> >
> > svg% ruby -e 'Dir.new("yyy").each {|f| p f}'
> > "."
> > ".."
> > "aaa"
> > svg%
> >
> > you must add `start_dir' for the test File::file?
> >
> > you can also look at Find::find
> >
> > svg% ruby -rfind -e 'Find.find("yyy") {|f| p f}'
> > "yyy"
> > "yyy/aaa"
> > svg%
> >
>
> Thanks for your help!
> Yes,I think Find was what I was initially searching for. Sth. like
> Find::prune would have been my next task...

For reasons of completeness: there is also Dir#[] like in

11:11:54 [temp]: ruby -e 'puts Dir["perl/**/*"]'
perl/a.wml
perl/att.pl
perl/bool.pl
perl/env-test.pl
perl/fun.pl
perl/hash.pl
perl/here.pl
perl/interp.pl
perl/method.pl
perl/pin.wml
perl/repl.pl
perl/replace.pl
perl/test-2.pl
perl/test.pl
perl/x
perl/x.html
perl/x.html~
perl/x.js
perl/x.jsf
perl/x.jsf~
perl/x.jsp
perl/x.jsp~
perl/x~

Another note: if you use File.join() to construct path names, the script
will work independent of platform. So the call would look like this then:

ruby -e 'puts Dir[File.join("perl", "**", "*")]'

Drawback of Dir[] is that the array can get quite huge and you can't stop
traversal in between as with find. Also, you can only use file name
pattern criteria for selecting the files but not others like the size or
modification time.

Kind regards

robert

Mohammad Khan

10/29/2004 3:11:00 PM

0

Wouldn't it be nice to have two more methods true? and false? like nil?
in Object.

n = NilClass.new
t = TrueClass.new
f = FalseClass.new

n.nil? -> true
t.nil? -> false
f.nil? -> false

n.true? -> false
t.true? -> true
f.true? -> false

n.false -> false
t.false -> false
f.false -> true


Thanks,
Mohammad



Bill Atkins

10/29/2004 3:12:00 PM

0

Why would that be nice? :)

Bill

On Sat, 30 Oct 2004 00:10:49 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:
> Wouldn't it be nice to have two more methods true? and false? like nil?
> in Object.
>
> n = NilClass.new
> t = TrueClass.new
> f = FalseClass.new
>
> n.nil? -> true
> t.nil? -> false
> f.nil? -> false
>
> n.true? -> false
> t.true? -> true
> f.true? -> false
>
> n.false -> false
> t.false -> false
> f.false -> true
>
> Thanks,
> Mohammad
>
>


Mohammad Khan

10/29/2004 3:17:00 PM

0

I prefer something like,

a.true?
not, a == true or a.class == TrueClass

And I think, most of us prefer to write code this way.
--
Mohammad



On Fri, 2004-10-29 at 11:12, Bill Atkins wrote:
> Why would that be nice? :)
>
> Bill
>
> On Sat, 30 Oct 2004 00:10:49 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:
> > Wouldn't it be nice to have two more methods true? and false? like nil?
> > in Object.
> >
> > n = NilClass.new
> > t = TrueClass.new
> > f = FalseClass.new
> >
> > n.nil? -> true
> > t.nil? -> false
> > f.nil? -> false
> >
> > n.true? -> false
> > t.true? -> true
> > f.true? -> false
> >
> > n.false -> false
> > t.false -> false
> > f.false -> true
> >
> > Thanks,
> > Mohammad
> >
> >
--
Mohammad Khan <mkhan@lextranet.com>
Legal Computer Solutions, Inc.



Bill Atkins

10/29/2004 3:21:00 PM

0

So:

if (a.method_call == b.method_call).true?

as opposed to:

if a.method_call == b.method_call

?

Is there some other situation where this would be an improvement?

Bill

On Sat, 30 Oct 2004 00:16:55 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:
> I prefer something like,
>
> a.true?
> not, a == true or a.class == TrueClass
>
> And I think, most of us prefer to write code this way.
> --
> Mohammad
>
>
>
>
> On Fri, 2004-10-29 at 11:12, Bill Atkins wrote:
> > Why would that be nice? :)
> >
> > Bill
> >
> > On Sat, 30 Oct 2004 00:10:49 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:
> > > Wouldn't it be nice to have two more methods true? and false? like nil?
> > > in Object.
> > >
> > > n = NilClass.new
> > > t = TrueClass.new
> > > f = FalseClass.new
> > >
> > > n.nil? -> true
> > > t.nil? -> false
> > > f.nil? -> false
> > >
> > > n.true? -> false
> > > t.true? -> true
> > > f.true? -> false
> > >
> > > n.false -> false
> > > t.false -> false
> > > f.false -> true
> > >
> > > Thanks,
> > > Mohammad
> > >
> > >
> --
> Mohammad Khan <mkhan@lextranet.com>
> Legal Computer Solutions, Inc.
>
>


James Gray

10/29/2004 3:22:00 PM

0

On Oct 29, 2004, at 10:16 AM, Mohammad Khan wrote:

> I prefer something like,
>
> a.true?
> not, a == true or a.class == TrueClass

if a
# ...
end

James Edward Gray II



Mohammad Khan

10/29/2004 3:29:00 PM

0

Bill,

I didn't get what you wanted to mean by your examples.

If nil? can be in DbObject
true? and false? can be in DbObject.

--
Mohammad



On Fri, 2004-10-29 at 11:20, Bill Atkins wrote:
> So:
>
> if (a.method_call == b.method_call).true?
>
> as opposed to:
>
> if a.method_call == b.method_call
>
> ?
>
> Is there some other situation where this would be an improvement?
>
> Bill
>
> On Sat, 30 Oct 2004 00:16:55 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:
> > I prefer something like,
> >
> > a.true?
> > not, a == true or a.class == TrueClass
> >
> > And I think, most of us prefer to write code this way.
> > --
> > Mohammad
> >
> >
> >
> >
> > On Fri, 2004-10-29 at 11:12, Bill Atkins wrote:
> > > Why would that be nice? :)
> > >
> > > Bill
> > >
> > > On Sat, 30 Oct 2004 00:10:49 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:
> > > > Wouldn't it be nice to have two more methods true? and false? like nil?
> > > > in Object.
> > > >
> > > > n = NilClass.new
> > > > t = TrueClass.new
> > > > f = FalseClass.new
> > > >
> > > > n.nil? -> true
> > > > t.nil? -> false
> > > > f.nil? -> false
> > > >
> > > > n.true? -> false
> > > > t.true? -> true
> > > > f.true? -> false
> > > >
> > > > n.false -> false
> > > > t.false -> false
> > > > f.false -> true
> > > >
> > > > Thanks,
> > > > Mohammad
> > > >
> > > >
> > --
> > Mohammad Khan <mkhan@lextranet.com>
> > Legal Computer Solutions, Inc.
> >
> >
--
Mohammad Khan <mkhan@lextranet.com>
Legal Computer Solutions, Inc.