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

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 :wink: but I couldn't find it :-((
Can someone guide me?

Michael

  files = Dir.new(start_dir).to_a[2..-1] #remove . and ..
  files.each do |f|
    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

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 :wink: but I couldn't find it :-((
Can someone guide me?

Michael

RTFS ... lol

···

On Thu, 2004-10-07 at 12:30, Michael Weller wrote:

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

ts wrote:

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

> files = Dir.new(start_dir).to_a[2..-1] #remove . and ..
> files.each do |f|
> 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

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

"Michael Weller" <michael@gutschi.de> schrieb im Newsbeitrag
news:41657516.4000602@gutschi.de...

ts wrote:
>>>>>>"M" == Michael Weller <michael@gutschi.de> writes:
>
>
> > files = Dir.new(start_dir).to_a[2..-1] #remove . and ..
> > files.each do |f|
> > 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

Why would that be nice? :slight_smile:

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

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? :slight_smile:

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.

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? :slight_smile:
>
> 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.

if a
# ...
end

James Edward Gray II

···

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

I prefer something like,

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

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? :slight_smile:
> >
> > 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.

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

if a
# ...
end

compare this,
a.false?
with
a == false or a.class == FalseClass

moreover,
if (not a)
..
end

will return true for a=nil and a=false

···

On Fri, 2004-10-29 at 11:22, James Edward Gray II wrote:

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

James Edward Gray II

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

Well, all Objects must be nil or non-nil, but the idea of true/false
doesn't apply to all objects.

My examples were just an attempt to figure out how these methods would
be useful.

Bill

···

On Sat, 30 Oct 2004 00:29:11 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:

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.

I'm sorry, but I don't understand why you think you need this latter.

foo if (a == false)

is sufficient. If you don't like that, then you can do:

foo if (FalseClass === a)

They're equivalent.

I don't really see anything that is added in terms of readability or
functionality by adding #true? and #false?.

-austin

···

On Sat, 30 Oct 2004 00:47:31 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:

compare this,
a.false?
with
a == false or a.class == FalseClass

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca
: as of this email, I have [ 5 ] Gmail invitations

Well, all Objects must be nil or non-nil, but the idea of true/false
doesn't apply to all objects.

I agree with you on this point.

I would ask,
If I would like to have true? and false?
what would be best class to put in?

My examples were just an attempt to figure out how these methods would
be useful.

Bill

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

And sorry for this logic

···

On Fri, 2004-10-29 at 11:48, Bill Atkins wrote:

On Sat, 30 Oct 2004 00:29:11 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:

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

Same way I can say,

a == nil or a.class == NilClass

why we have a nil? ?
of course, to have more readability.

···

On Fri, 2004-10-29 at 13:43, Austin Ziegler wrote:

On Sat, 30 Oct 2004 00:47:31 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:
> compare this,
> a.false?
> with
> a == false or a.class == FalseClass

I'm sorry, but I don't understand why you think you need this latter.

foo if (a == false)

is sufficient. If you don't like that, then you can do:

foo if (FalseClass === a)

They're equivalent.

I don't really see anything that is added in terms of readability or
functionality by adding #true? and #false?.

-austin

--
Mohammad

But it must be responded to by all classes.

  # true?&false?.rb
  # As suggested by Mohammad Khan

  class Object
    def true? ; false ; end
    def false? ; false ; end
  end

  class TrueClass
    def true? ; true ; end
  end

  class FalseClass
    def false? ; true ; end
  end

Is this what you had in mind?

T.

···

On Friday 29 October 2004 11:55 am, Mohammad Khan wrote:

On Fri, 2004-10-29 at 11:48, Bill Atkins wrote:
> Well, all Objects must be nil or non-nil, but the idea of true/false
> doesn't apply to all objects.

I agree with you on this point.

I would ask,
If I would like to have true? and false?
what would be best class to put in?

Mohammad Khan wrote:

compare this,
a.false?
with
a == false or a.class == FalseClass

I'm sorry, but I don't understand why you think you need this latter.

foo if (a == false)

is sufficient. If you don't like that, then you can do:

foo if (FalseClass === a)

They're equivalent.

I don't really see anything that is added in terms of readability or
functionality by adding #true? and #false?.

-austin

Same way I can say,

a == nil or a.class == NilClass

why we have a nil? ?

The point Austin was making was, if nil? was removed why would you need to write

a == nil or a.class == NilClass

rather than just

a == nil

Likewise, why do you need to write

a == true or a.class == TrueClass

rather than

a == true

?

···

On Fri, 2004-10-29 at 13:43, Austin Ziegler wrote:

On Sat, 30 Oct 2004 00:47:31 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:

--
Mark Sparshatt

if (expression)
   block # 1
end

block # 1 would be executed if expression is true.

Watch this,
a = 5

# 2
if (a)
  block # 2
end

# 3
if (true)
  block # 3
end

# 4
if (a == true)
  block # 4
end

# 5
if (a.class == TrueClass)
  block # 5
end

# 6
if (not a.nil?)
  block # 6
end

Ofcource, block # 4 and #5 would not execute.
Why would block # 2 ?

So, isn't #6 more logical than #2.
Didn't we wanted to mean #6 when we wrote #2

···

--
Mohammad

> > Well, all Objects must be nil or non-nil, but the idea of true/false
> > doesn't apply to all objects.
>
> I agree with you on this point.
>
> I would ask,
> If I would like to have true? and false?
> what would be best class to put in?

But it must be responded to by all classes.

  # true?&false?.rb
  # As suggested by Mohammad Khan

  class Object
    def true? ; false ; end
    def false? ; false ; end
  end

In my mind, I had this:

class Object
    def true?
        return self == true ? true : false
    end

    def false?
        return self == false ? true : false
    end
end

irb(main):009:0> n = nil
=> nil
irb(main):010:0> n.nil?
=> true
irb(main):011:0> n.true?
=> false
irb(main):012:0> n.false?
=> false
irb(main):013:0> t = true
=> true
irb(main):015:0> t.true?
=> true
irb(main):016:0> t.false?
=> false
irb(main):017:0> f = false
=> false
irb(main):018:0> f.nil?
=> false
irb(main):019:0> f.true?
=> false
irb(main):020:0> f.false?
=> true

···

On Fri, 2004-10-29 at 12:01, trans. (T. Onoma) wrote:

On Friday 29 October 2004 11:55 am, Mohammad Khan wrote:
> On Fri, 2004-10-29 at 11:48, Bill Atkins wrote:

  class TrueClass
    def true? ; true ; end
  end

  class FalseClass
    def false? ; true ; end
  end

Is this what you had in mind?

T.

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