File checks....

G

GGarramuno

Is there anything in Ruby similar to Perl's if -r, -x, etc. switches?
These switches work with variables as if they were files and they are
used in simple, silent and quick constructs like:
if -r $file {
print "$file is readable\n";
}

Or am I forced to write all the code of File.new() and handle the
rescue exceptions myself?
 
M

Michael W Thelen

* GGarramuno said:
Is there anything in Ruby similar to Perl's if -r, -x, etc. switches?
These switches work with variables as if they were files and they are
used in simple, silent and quick constructs like:
if -r $file {
print "$file is readable\n";
}

You can use the FileTest methods, e.g.

FileTest.executable?("/bin/ls") #=> true
FileTest.executable?("/etc/passwd") #=> false

Some documentation is in 'ri', or on the Web here:
http://www.rubycentral.com/book/ref_m_filetest.html

-- Mike
 
J

Joel VanderWerf

GGarramuno said:
Is there anything in Ruby similar to Perl's if -r, -x, etc. switches?
These switches work with variables as if they were files and they are
used in simple, silent and quick constructs like:
if -r $file {
print "$file is readable\n";
}

Or am I forced to write all the code of File.new() and handle the
rescue exceptions myself?

irb(main):001:0> test ?r, "."
=> true

The method is Kernel#test. For details, see rdoc or

http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.test
 
G

Gennady

GGarramuno said:
Is there anything in Ruby similar to Perl's if -r, -x, etc. switches?
These switches work with variables as if they were files and they are
used in simple, silent and quick constructs like:
if -r $file {
print "$file is readable\n";
}

Or am I forced to write all the code of File.new() and handle the
rescue exceptions myself?

Check out methods of module FileTest p FileTest.methods). Class File
extends this module, so you can write:

File.readable? "afile"

If "aFile" does not exist, File.readable? returns false.

Gennady.
 
G

Gavin Sinclair

Is there anything in Ruby similar to Perl's if -r, -x, etc. switches?

Time to read the PickAxe:

www.ruby-doc.org

http://www.amazon.com/exec/obidos/tg/detail/-/0201710897/
These switches work with variables as if they were files and they are
used in simple, silent and quick constructs like:
if -r $file {
print "$file is readable\n";
}

Or am I forced to write all the code of File.new() and handle the
rescue exceptions myself?

See the FileTest module (e.g. in 'ri')

Cheers,
Gavin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top