[File.readable?] How to make it work?

I’m trying to make my own program that can help me manage my
files/folders faster than Windows command prompt but I have ran across a
problem.

The problem is if a user tries to open a folder while the program is in
“open text file” mode, it will close itself.

Code: http://pastebin.com/uFtpji0H

My question to you is, why is this File.readable not working against
folders? Is my code bad? Help me.

Dear Cynic Limbu,

You can test if the “filename” is a file with File::file?

Look at a modified version of your pastebin.

http://pastebin.com/NkUZt8kq

Abinoam Jr.

On Tue, Jan 28, 2014 at 2:50 PM, Robert K.

On Tue, Jan 28, 2014 at 7:37 PM, Abinoam Jr. [email protected] wrote:

Dear Cynic Limbu,

You can test if the “filename” is a file with File::file?

Generally Pathname comes handy in such situations because it supports
all the file operations and tests with a nicer syntax.
http://ruby-doc.org/stdlib-1.9.3/libdoc/pathname/rdoc/index.html

Kind regards

robert

I’d like to thank all of you for answering my question, it looks like
I’m still far away from being able to create my own programs because I’m
still a newbie in ruby.

why we can’t open a dir , and return a file list .

a=File.open(".")
a.each_line{|f| p open(f) }

On Tue, Jan 28, 2014 at 3:46 PM, cynic limbu [email protected]
wrote:

I’m trying to make my own program that can help me manage my
files/folders faster than Windows command prompt but I have ran across a
problem.

The problem is if a user tries to open a folder while the program is in
“open text file” mode, it will close itself.

What error are you seeing? Is it this one?

$ ruby -e ‘File.open("."){|io|io.each_line {|l| puts l}}’
-e:1:in each_line': Is a directory - . (Errno::EISDIR) from -e:1:inblock in ’
from -e:1:in open' from -e:1:in

You usually cannot open folders like files. That’s why there is
Dir.open():
http://www.ruby-doc.org/core-1.9.3/Dir.html#method-c-open

Code: http://pastebin.com/uFtpji0H

Btw. using string interpolation for strings is superfluous and
inefficient.

My question to you is, why is this File.readable not working against
folders? Is my code bad? Help me.

It is:

$ ruby -e ‘p File.readable?(".")’
true

File.readable checks permissions but File.open checks the type of
filesystem object. That’s not the same.

Cheers

robert

On Wed, Jan 29, 2014 at 8:06 AM, Kk Kk [email protected] wrote:

why we can’t open a dir , and return a file list .

a=File.open(".")
a.each_line{|f| p open(f) }

Because a directory does not consist of lines or arbitrary content. A
directory consists of data of a fixed structure. See also
http://linux.die.net/man/3/opendir

As I mentioned earlier, there is Dir.open

irb(main):004:0> system %w{touch /tmp/foo}
=> true
irb(main):005:0> Dir[’/tmp/
’]
=> ["/tmp/foo"]
irb(main):006:0> Dir.open(’/tmp’) {|d| d.each {|e| p e}}
“.”
“…”
“foo”
=> #Dir:/tmp

Regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs