From: Robert Klemme Date: 2006-05-16T05:27:00+09:00 Subject: Re: Search an array 2006/5/15, Berger, Daniel : > > -----Original Message----- > > From: list-bounce@example.com > > [mailto:list-bounce@example.com] On Behalf Of Patrick Spence > > Sent: Monday, May 15, 2006 9:59 AM > > To: ruby-talk ML > > Subject: Search an array > > > > > > > > Given the following scenario... > > > > $0 = "C:/mhiqTest/testAdoConnection.rb" > > > > $@[0] = "c:/ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb:57:in > > `connect'" $@[1] = > > "c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:424:in `connect'" > > $@[2] = "c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:215:in > > `connect'" $@[3] = "C:/mhiqTest/testAdoConnection.rb:16" > > > > How can I search the callstack array; $@, for the element > > that contains > > the currently running program; $0? The idea is to log all the > > errors to > > a SQL Server database, the name of the program, line number where the > > error occurred, error message, etc. > > > > Thanks! > > Why look in $@? You already know what $0 is. He needs to find the entry with the name to get the line number. Here's another nice solution: prog = "C:/mhiqTest/testAdoConnection.rb" stack = [ "c:/ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb:57:in `connect'", "c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:424:in `connect'", "c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:215:in `connect'", "C:/mhiqTest/testAdoConnection.rb:16", ] rx = Regexp.new(Regexp.escape(prog) + ":(\\d+)") stack.inject(nil) {|nr, frame| rx =~ frame ? $1.to_i : nr } :-)) Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/