preventing irb exiting out on errors

17 views
Skip to first unread message

Robert Citek

unread,
Nov 10, 2009, 1:29:02 PM11/10/09
to stl...@googlegroups.com
Is there a way to prevent irb from exiting whenever I mess up a SQL statement?

For example, if I do a query and misspell foobar as foober:

irb(main):007:0> require 'sqlite3'
=> false

irb(main):008:0> db = SQLite3::Database.new("test.db")
=> #<SQLite3::Database:0xb7b28e34
@driver=#<SQLite3::Driver::Native::Driver:0xb7b28d94 @trace={},
@busy_handler={}, @authorizer={}, @callback_data={}>,
@transaction_active=false, @closed=false,
@handle=#<SWIG::TYPE_p_sqlite3:0xb7b28c40>, @translator=nil,
@statement_factory=SQLite3::Statement, @type_translation=false,
@results_as_hash=false>

irb(main):009:0> db.execute2("select * from foobar")
=> [["foo", "bar"], ["hello", "1"], ["world", "2"]]

irb(main):010:0> db.execute2("select * from foober")
/usr/lib/ruby/1.8/sqlite3/errors.rb:94:in `check': no such table:
foober (SQLite3::SQLException)
from /usr/lib/ruby/1.8/sqlite3/statement.rb:71:in `initialize'
from /usr/lib/ruby/1.8/sqlite3/database.rb:184:in `new'
from /usr/lib/ruby/1.8/sqlite3/database.rb:184:in `prepare'
from /usr/lib/ruby/1.8/sqlite3/database.rb:232:in `execute2'
from (irb):10:in `irb_binding'
from /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
from /usr/lib/ruby/1.8/irb/workspace.rb:52
$

I would prefer that the error message get shown and then continue inside irb.

Thanks in advance to any pointers.

Regards,
- Robert

Craig Buchek

unread,
Nov 10, 2009, 2:27:57 PM11/10/09
to Saint Louis Ruby Users Group
db.execute2("select * from foober") rescue nil

Although I'm not clear on why it would exit IRB on an error. You could
also rescue the exception and print it out, rescue on specific errors,
create a method to wrap the execute + rescue, and even replace
execute2 with a wrapped version.

Craig

Robert Citek

unread,
Nov 24, 2009, 3:03:12 PM11/24/09
to stl...@googlegroups.com
On Tue, Nov 10, 2009 at 2:27 PM, Craig Buchek <craig....@gmail.com> wrote:
> db.execute2("select * from foober") rescue nil
>
> Although I'm not clear on why it would exit IRB on an error.

That didn't work for me. irb still exits on errors.

irb transcript:

irb(main):001:0> require 'sqlite3'
=> true
irb(main):002:0> db = SQLite3::Database.new( "test.db" )
=> #<SQLite3::Database:0xb7b279bc
@driver=#<SQLite3::Driver::Native::Driver:0xb7b1e628 @trace={},
@busy_handler={}, @authorizer={}, @callback_data={}>,
@transaction_active=false, @closed=false,
@handle=#<SWIG::TYPE_p_sqlite3:0xb7b1e59c>, @translator=nil,
@statement_factory=SQLite3::Statement, @type_translation=false,
@results_as_hash=false>
irb(main):003:0> db.execute2("select * from foober") rescue nil
/usr/lib/ruby/1.8/sqlite3/errors.rb:94:in `check': no such table:
foober (SQLite3::SQLException)
from /usr/lib/ruby/1.8/sqlite3/statement.rb:71:in `initialize'
from /usr/lib/ruby/1.8/sqlite3/database.rb:184:in `new'
from /usr/lib/ruby/1.8/sqlite3/database.rb:184:in `prepare'
from /usr/lib/ruby/1.8/sqlite3/database.rb:232:in `execute2'
from (irb):3:in `irb_binding'
from /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
from /usr/lib/ruby/1.8/irb/workspace.rb:52
$

Regards,
- Robert

Craig Buchek

unread,
Nov 24, 2009, 4:13:37 PM11/24/09
to Saint Louis Ruby Users Group
You've really got 2 problems here, and I'm not sure how related they
are. First, the rescue clause isn't working. On my system, that
statement returns nil. Second, your IRB is exiting on errors. My IRB
session prints the traceback if I remove the rescue clause, but then
gives me another IRB prompt.

Do you have anything pertinent in your .irbrc file? What version of
Ruby are you running, and what OS/version?

Craig

Robert Citek

unread,
Nov 24, 2009, 5:07:36 PM11/24/09
to stl...@googlegroups.com
On Tue, Nov 24, 2009 at 4:13 PM, Craig Buchek <craig....@gmail.com> wrote:
> You've really got 2 problems here, and I'm not sure how related they
> are. First, the rescue clause isn't working. On my system, that
> statement returns nil. Second, your IRB is exiting on errors. My IRB
> session prints the traceback if I remove the rescue clause, but then
> gives me another IRB prompt.
>
> Do you have anything pertinent in your .irbrc file?

Don't have one, yet. Was just getting to that chapter in the pickaxe
book, when I inadvertently packed it in a box for moving. I'll
probably find it later tonight.

> What version of Ruby are you running, and what OS/version?

$ (set -x ; ruby -v ; irb -v ; lsb_release -a )
+ ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
+ irb -v
irb 0.9.5(05/04/13)
+ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 8.04.3 LTS
Release: 8.04
Codename: hardy

Thanks for the feedback.

Regards,
- Robert

Ed Howland

unread,
Nov 24, 2009, 6:22:34 PM11/24/09
to stl...@googlegroups.com
I confirmed Craig's behavior on 1.9.1 and 1.8.7 on Mac OSX and 1.8.7
on Ubuntu 9.10. I also tested on a Turnkey RoR VM appliance using Ruby
1.8.6 on Ubuntu 8.04 (LTS).

The only thing I can see is Robert is running some version of 1.8 that
was installed via a package manager (like apt-get). I'm guessing that
this is his problem. On Ubuntu, at least, prior to 9.10, I've
recommended installing ruby from source, not via apt-get. You get
older, less stable releases w/o common features like GNU readline.

Upgrade Ubuntu, install ruby from source or try upgrading to 1.9,

BTW, Turnkey appliances are cool because they come as just .iso files
that you can install in your fave VM app or burn to a CD. They are
all based on Ubuntu 8.04 so they should also boot as a live CD. I use
them to test running capistrano scripts before I push them live,

Regards,
Ed
--
Ed Howland
http://greenprogrammer.wordpress.com
http://twitter.com/ed_howland
Reply all
Reply to author
Forward
0 new messages