Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Hiding app.run in begin-rescue block: pros & cons?

Reply
Thread Tools

Hiding app.run in begin-rescue block: pros & cons?

 
 
Richard Lionheart
Guest
Posts: n/a
 
      07-15-2004
Hi All,

Upon asking a friend to review my code, he suggested that many people hide
app.run in a begin-rescue block. I assume one might code:

begin
app.run
rescue => err
puts "Application failed:", err
end

(which I got from Hal Fulton's "The Ruby Way")

What are the pros and cons of this approach?

Regards,
Richard


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.719 / Virus Database: 475 - Release Date: 7/13/2004


 
Reply With Quote
 
 
 
 
Richard Lionheart
Guest
Posts: n/a
 
      07-15-2004
Hi All,

I apologize for not testing the two approaches myself before posting the
question. My take on this now is:

Use begin-rescue when code is released for production, because it prevents
a "blizzard" of trace-back messages which would dismay users.

Use plain app.run when in development mode, because the trace-back is
generally helpful.

Anyone have other ideas?

Regards,
Richard


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.719 / Virus Database: 475 - Release Date: 7/13/2004


 
Reply With Quote
 
 
 
 
Ara.T.Howard
Guest
Posts: n/a
 
      07-15-2004
On Thu, 15 Jul 2004, Richard Lionheart wrote:

> Hi All,
>
> I apologize for not testing the two approaches myself before posting the
> question. My take on this now is:
>
> Use begin-rescue when code is released for production, because it prevents
> a "blizzard" of trace-back messages which would dismay users.
>
> Use plain app.run when in development mode, because the trace-back is
> generally helpful.
>
> Anyone have other ideas?
>
> Regards,
> Richard



require 'logger'


logger = Logger.new STDOUT

....
....


begin
app.run
rescue Exception => e
logger.debug{ e }
logger.fatal{ 'application failed' }
end


print stack trace only when debugging, give users the ability to turn
debugging on with a command line switch.

-a
--
================================================== =============================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
================================================== =============================
 
Reply With Quote
 
NAKAMURA, Hiroshi
Guest
Posts: n/a
 
      07-16-2004
Hi,

Ara.T.Howard wrote:
> require 'logger'


> logger = Logger.new STDOUT


> begin
> app.run
> rescue Exception => e
> logger.debug{ e }
> logger.fatal{ 'application failed' }
> end
>
>
> print stack trace only when debugging, give users the ability to turn
> debugging on with a command line switch.


And here's a sample of class Logger::Application which might be usable
for original purpose?

Regards,
// NaHi

require 'logger'

class App < Logger::Application
def initialize(level)
super
@log.level = Logger.const_get(level.upcase)
end

def run
@log.debug { "debug msg" }
@log.info { "info msg" }
if @log.fatal? # true iif fatal msg is allowed to dump
raise # an exception is logged as a fatal msg
end
0 # exit status
end
end

# test

puts "<<DEBUG"
p App.new("DEBUG").start
puts "DEBUG"

puts

puts "<<INFO"
p App.new("INFO").start
puts "INFO"

puts

puts "<<UNKNOWN"
p App.new("UNKNOWN").start
puts "UNKNOWN"



 
Reply With Quote
 
daz
Guest
Posts: n/a
 
      07-16-2004

NAKAMURA, Hiroshi wrote:
>
> And here's a sample of class Logger::Application [...]
>
>[snip useful sample]



Plenty nice !!

Very well documented in <rubydir>/lib/logger.rb -AND/OR- ...
<http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/lib/logger.rb?rev=1.5.2.4;content-type=text%2Fplain>


<<DEBUG
I, [2004-07-16T04:45:30.320000 #613397] INFO -- DEBUG: Start of DEBUG.
D, [2004-07-16T04:45:30.320000 #613397] DEBUG -- DEBUG: debug msg
I, [2004-07-16T04:45:30.320000 #613397] INFO -- DEBUG: info msg
F, [2004-07-16T04:45:30.320000 #613397] FATAL -- DEBUG: Detected an exception. Stopping ... (RuntimeError)
C:/TEMP/rb1022.TMP:13:in `run'
D:/RUBY/SRC_CVSINST/lib/ruby/1.9/logger.rb:684:in `start'
C:/TEMP/rb1022.TMP:22
I, [2004-07-16T04:45:30.320000 #613397] INFO -- DEBUG: End of DEBUG. (status: -1)
-1
DEBUG

<<INFO
I, [2004-07-16T04:45:30.430000 #613397] INFO -- INFO: Start of INFO.
I, [2004-07-16T04:45:30.430000 #613397] INFO -- INFO: info msg
F, [2004-07-16T04:45:30.430000 #613397] FATAL -- INFO: Detected an exception. Stopping ... (RuntimeError)
C:/TEMP/rb1022.TMP:13:in `run'
D:/RUBY/SRC_CVSINST/lib/ruby/1.9/logger.rb:684:in `start'
C:/TEMP/rb1022.TMP:28
I, [2004-07-16T04:45:30.430000 #613397] INFO -- INFO: End of INFO. (status: -1)
-1
INFO

<<UNKNOWN
0
UNKNOWN


> Regards,
> // NaHi
>


:daz



 
Reply With Quote
 
Alexey Verkhovsky
Guest
Posts: n/a
 
      07-17-2004
irb(main):001:0> s = "#{aaa}"
NameError: undefined local variable or method `aaa' for main:Object
from (irb):1
from :0
irb(main):002:0> s = %s{#{aaa}}
/usr/local/lib/ruby/1.9/irb/ruby-token.rb:101:in `Token': undefined
method `ancestors' for nil:NilClass (NoMethodError)
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:995:in
`identify_string'
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:914:in
`identify_quotation'
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:667:in `lex_int2'
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:664:in `call'
from /usr/local/lib/ruby/1.9/irb/slex.rb:234:in `match_io'
from /usr/local/lib/ruby/1.9/irb/slex.rb:219:in `match_io'
from /usr/local/lib/ruby/1.9/irb/slex.rb:73:in `match'
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:277:in `token'
... 7 levels...
from /usr/local/lib/ruby/1.9/irb.rb:70:in `start'
from /usr/local/lib/ruby/1.9/irb.rb:69:in `catch'
from /usr/local/lib/ruby/1.9/irb.rb:69:in `start'
from /usr/local/bin/irb:13
[alex@dhcp-89-2 alex]$ ruby -v
ruby 1.9.0 (2004-07-0 [i686-linux]

------

Meantime,

[alex@dhcp-89-2 alex]$ ruby
s = %s{#{aaa}}
p s
:"\#{aaa}"




 
Reply With Quote
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      07-17-2004
Hi,

In message "Bug? String literal %s{#{a}} cause irb to abort"
on 04/07/17, Alexey Verkhovsky <(E-Mail Removed)> writes:

|irb(main):002:0> s = %s{#{aaa}}
|/usr/local/lib/ruby/1.9/irb/ruby-token.rb:101:in `Token': undefined
|method `ancestors' for nil:NilClass (NoMethodError)
| from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:995:in
|`identify_string'
| from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:914:in
|`identify_quotation'

It's a bug in irb. Thank you for the report.

matz.


 
Reply With Quote
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      07-17-2004
Hi,

At Sat, 17 Jul 2004 09:05:08 +0900,
Alexey Verkhovsky wrote in [ruby-talk:106691]:
> irb(main):001:0> s = "#{aaa}"
> NameError: undefined local variable or method `aaa' for main:Object
> from (irb):1
> from :0
> irb(main):002:0> s = %s{#{aaa}}
> /usr/local/lib/ruby/1.9/irb/ruby-token.rb:101:in `Token': undefined
> method `ancestors' for nil:NilClass (NoMethodError)
> from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:995:in
> `identify_string'


Does this help?


Index: lib/irb/ruby-lex.rb
================================================== =================
RCS file: /cvs/ruby/src/ruby/lib/irb/ruby-lex.rb,v
retrieving revision 1.22
diff -U2 -p -d -r1.22 ruby-lex.rb
--- lib/irb/ruby-lex.rb 4 Oct 2003 17:51:09 -0000 1.22
+++ lib/irb/ruby-lex.rb 17 Jul 2004 04:35:34 -0000
@@ -325,4 +325,5 @@ class RubyLex
"\`" => TkDXSTRING,
"\/" => TkDREGEXP,
+ ":" => TkDSYMBOL,
}

@@ -974,5 +975,5 @@ class RubyLex
if @quoted == ch and nest == 0
break
- elsif @ltype != "'" && @ltype != "]" and ch == "#"
+ elsif ! "']:".index(@ltype) and ch == "#"
subtype = true
elsif ch == '\\' #'
Index: lib/irb/ruby-token.rb
================================================== =================
RCS file: /cvs/ruby/src/ruby/lib/irb/ruby-token.rb,v
retrieving revision 1.5
diff -U2 -p -d -r1.5 ruby-token.rb
--- lib/irb/ruby-token.rb 5 Aug 2003 03:08:16 -0000 1.5
+++ lib/irb/ruby-token.rb 17 Jul 2004 04:35:26 -0000
@@ -170,4 +170,5 @@ module RubyToken
[:TkDXSTRING, TkNode],
[:TkDREGEXP, TkNode],
+ [:TkDSYMBOL, TkNode],
[:TkNTH_REF, TkNode],
[:TkBACK_REF, TkNode],


--
Nobu Nakada


 
Reply With Quote
 
Alexey Verkhovsky
Guest
Posts: n/a
 
      07-17-2004
On Sat, 2004-07-17 at 08:09, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Does this help?
> ... diff patch to lib/irb/ruby-lex.rb and lib/irb/ruby-token.rb ...


Hi,

The bug I reported earlier is not reproduced, but there is some other
interesting behavior (three years I've spent in system test just won't
go away )

[alex@dhcp-89-2 ruby]$ irb
irb(main):001:0> a = %s{#"aaa"} // previously reported
=> :"\#\"aaa\"" // now behaves as expected

// Another odd behavior
irb(main):002:0> a = %s
irb(main):003:0:
// expected: SyntaxError
// actual: an "empty symbol" value is assigned to a

irb(main):004:0> a
SyntaxError: compile error
(irb):3: empty symbol literal
(irb):4: syntax error
from (irb):4
from :0
// expected: SyntaxError
irb(main):009:0> a.inspect
SyntaxError: compile error
(irb):8: empty symbol literal
(irb):9: syntax error
a.inspect
^
from (irb):9
from :0
irb(main):010:0> a.class
=> Symbol


// empty symbol is illegal
irb(main):011:0> a = %s{}
SyntaxError: compile error
(irb):11: empty symbol literal
from (irb):11
from :0


Best regards,
Alexey Verkhovsky



 
Reply With Quote
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      07-17-2004
Hi,

At Sat, 17 Jul 2004 22:40:34 +0900,
Alexey Verkhovsky wrote in [ruby-talk:106711]:
> // Another odd behavior
> irb(main):002:0> a = %s
> irb(main):003:0:
> // expected: SyntaxError
> // actual: an "empty symbol" value is assigned to a


It is actually same as:

$ ruby -e '%s' -e ''
-e:2: empty symbol literal

i.e., a symbol litral terminated by \n.

> irb(main):004:0> a
> SyntaxError: compile error


But I don't know why it delays until the next line; this would
be an irb issue.

--
Nobu Nakada


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script for Hiding/Un-Hiding Text On Click Ste Javascript 41 08-01-2007 02:35 PM
Need a pros help =?Utf-8?B?Q2hyaXM=?= ASP .Net 1 01-31-2005 09:46 PM
I really need a pros help here =?Utf-8?B?Q2hyaXM=?= ASP .Net 5 12-10-2004 04:27 PM
PSL pros and cons Kumar Vijay Mishra VHDL 2 10-02-2004 12:34 AM
Pros and Cons about automatic increase in MS SQL 2000 Benny ASP .Net 1 03-01-2004 10:21 PM



Advertisments