From: Robert Klemme Date: 2003-03-17T18:21:04+09:00 Subject: Re: Ruby / Eiffel ? "Jason M Jurkowski ;002;icsg6;" schrieb im Newsbeitrag news:Pine.GSO.4.53.0303132238200.20279@utah.cs.rit.edu... > i've coded up a ruby "assertion" for myself as follows: > > def assert(conditional) > begin > raise Exception, "assertion failed" if not conditional > rescue Exception => e > filename = e.backtrace()[1].split(/:/)[0] > lineno = e.backtrace()[1].split(/:/)[1].to_i() > file = File::new(filename) > puts e.message+": "+file.readlines()[lineno-1] > exit! 1 > end > end Throwing and immediately catching an exception is superfluous. I like this more: def assert(cond) unless cond filename, lineno = caller[0].split(/:/) raise ( File.open( filename ) do |f| "assertion failed at #{filename}:#{lineno}: #{ f.readlines()[ lineno.to_i - 1 ] }" end ) end end Note that the exception trace that ruby prints for uncaught exceptions does contain the file and line number already. So if you leave out printing of the line itself, this is sufficient: def assert(cond) raise "assertion failed" unless cond end And for delivered code def assert(cond); end regards robert