What is the clearest way to open a file and do "rescue" when it cannot be opened in Ruby -


i dealing issue using following code

begin   file.open(filename, 'r') rescue   print "failed open #{filename}\n"   exit end 

is there way more perl 'open (in, $filename) || die "failed open $filename\n"' thanks.

file.open("doesnotexist.txt", 'r') 

is enough. if file not exist, raise exception. not caught, program exits.

# =>test6.rb:1:in `initialize': no such file or directory @ rb_sysopen - doesnotexist.txt (errno::enoent) 

Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -