Forum: Ruby Search an array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Patrick S. (Guest)
on 2006-05-15 19:58
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!
Mike F. (Guest)
on 2006-05-15 20:08
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:in `connect'",
 "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"]
Patrick S. (Guest)
on 2006-05-15 20:35
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:in `connect'",
>  "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!
Mike F. (Guest)
on 2006-05-16 00:41
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:in `connect'",
>>  "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
This topic is locked and can not be replied to.