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

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -