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

···

--
Posted via http://www.ruby-forum.com/.

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:in `block in <main>'
        from -e:1:in `open'
        from -e:1:in `<main>'

You usually cannot open folders like files. That's why there is Dir.open():

Code: File.readable? How To Make It Work? - Pastebin.com

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 Tue, Jan 28, 2014 at 3:46 PM, cynic limbu <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

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

···

--
Posted via http://www.ruby-forum.com/.

Dear Cynic Limbu,

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

Look at a modified version of your pastebin.

Abinoam Jr.

···

On Tue, Jan 28, 2014 at 2:50 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Tue, Jan 28, 2014 at 3:46 PM, cynic limbu <lists@ruby-forum.com> 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:in `block in <main>'
        from -e:1:in `open'
        from -e:1:in `<main>'

You usually cannot open folders like files. That's why there is Dir.open():
Class: Dir (Ruby 1.9.3)

Code: File.readable? How To Make It Work? - Pastebin.com

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

On Tue, Jan 28, 2014 at 7:37 PM, Abinoam Jr. <abinoam@gmail.com> wrote:

Dear Cynic Limbu,

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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.

···

--
Posted via http://www.ruby-forum.com/.

Because a directory does not consist of lines or arbitrary content. A
directory consists of data of a fixed structure. See also

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

···

On Wed, Jan 29, 2014 at 8:06 AM, Kk Kk <lists@ruby-forum.com> wrote:

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

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/