Search an array


#1

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:inconnect’”
$@[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!


#2

Patrick S. wrote:

Given the following scenario…
[…]
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.

Slightly modified but basically the same as your scenario, searching $a
rather than $@ since it’s not amenable to being set directly apparently.

irb(main):018:0> pp $0
“C:/mhiqTest/testAdoConnection.rb”
=> nil
irb(main):019:0> pp $a
[“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:inconnect’”,
“c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:215:in `connect’”,
“C:/mhiqTest/testAdoConnection.rb:16”]
=> nil
irb(main):020:0> $a.grep( %r/^#$0/ )
=> [“C:/mhiqTest/testAdoConnection.rb:16”]


#3

Mike F. wrote:

irb(main):018:0> pp $0
“C:/mhiqTest/testAdoConnection.rb”
=> nil
irb(main):019:0> pp $a
[“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:inconnect’”,
“c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:215:in `connect’”,
“C:/mhiqTest/testAdoConnection.rb:16”]
=> nil
irb(main):020:0> $a.grep( %r/^#$0/ )
=> [“C:/mhiqTest/testAdoConnection.rb:16”]

What is “pp”? BTW, I’m very new to Ruby!


#4

Patrick S. wrote:

Mike F. wrote:

irb(main):018:0> pp $0
“C:/mhiqTest/testAdoConnection.rb”
=> nil
irb(main):019:0> pp $a
[“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:inconnect’”,
“c:/ruby/lib/ruby/site_ruby/1.8/dbi.rb:215:in `connect’”,
“C:/mhiqTest/testAdoConnection.rb:16”]
=> nil
irb(main):020:0> $a.grep( %r/^#$0/ )
=> [“C:/mhiqTest/testAdoConnection.rb:16”]

What is “pp”? BTW, I’m very new to Ruby!

It’s the pretty printing library. I have it required in my ~/.irbrc so
it’s always available in irb sessions (as is the YAML module so I can
also ‘print foo.to_yaml’)

http://www.ruby-doc.org/stdlib/libdoc/pp/rdoc/index.html