From: Daniel Berger Date: 2002-10-22T06:38:13+09:00 Subject: Re: functional Ruby equiv to this perl snippet Mirian Crzig Lennox wrote: > On Tue, 22 Oct 2002 05:54:49 +0900, Bob X wrote: > > > >Cool..but what if serverlist.txt does not exist? The one thing I do like > >about Perl is the "open file or die" test. I believe that I am "stuck" with > >a rescue clause which is sometimes confusing for me (a newbie) to use when > >it is located within a "def". > > You can do something very similar in Ruby, using "fail" instead of "die". > For example: > > def open_file name > f = File.open(name) or fail "couldn't open #{name}" > return f > end > > --Mirian Note that this isn't strictly necessary, unless you just want to provide your own error message. Ruby will die at that point anyway if the file doesn't exist (and tell you why). No need to test. f = File.open("blah") # Results in this: # in `open': No such file or directory - "blah" (Errno::ENOENT) Regards, Dan