From: dblack@... Date: 2002-10-22T08:44:18+09:00 Subject: Re: functional Ruby equiv to this perl snippet Hi -- On Tue, 22 Oct 2002, Bob X wrote: > "Daniel Berger" wrote in message > news:3DB473B8.9454B953@qwest.com... > > > > > > 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 > > > I would want to provide my own error message. I did not see the "fail" > option...grrrrr. "or fail" won't work the way "or die" does in Perl; you need to rescue, because you're catching an exception (not just branching on a false return value). Something like: f = File.open("blah") rescue fail "That did not work" David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav