落書きノート

ふと自分が気になった事を書いてます

Ruby 資格技術者問題集

今回はこんな感じです。

irb(main):001:0> begin
irb(main):002:1* 1/0
irb(main):003:1> rescue ZeroDivisionError => e
irb(main):004:1> p e.backtrace
irb(main):005:1> end
["(irb):2:in `/'", "(irb):2:in `irb_binding'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/workspace.rb:86:in `eval'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/workspace.rb:86:in `evaluate'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/context.rb:379:in `evaluate'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:489:in `block (2 levels) in eval_input'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:623:in `signal_status'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:486:in `block in eval_input'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:245:in `block (2 levels) in each_top_level_statement'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:231:in `loop'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:231:in `block in each_top_level_statement'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:230:in `catch'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:230:in `each_top_level_statement'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:485:in `eval_input'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:395:in `block in start'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:394:in `catch'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:394:in `start'", "/Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'"]
=> ["(irb):2:in `/'", "(irb):2:in `irb_binding'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/workspace.rb:86:in `eval'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/workspace.rb:86:in `evaluate'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/context.rb:379:in `evaluate'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:489:in `block (2 levels) in eval_input'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:623:in `signal_status'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:486:in `block in eval_input'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:245:in `block (2 levels) in each_top_level_statement'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:231:in `loop'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:231:in `block in each_top_level_statement'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:230:in `catch'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb/ruby-lex.rb:230:in `each_top_level_statement'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:485:in `eval_input'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:395:in `block in start'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:394:in `catch'", "/Users/hirotoshi/.rbenv/versions/2.2.2/lib/ruby/2.2.0/irb.rb:394:in `start'", "/Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'"]
irb(main):006:0> begin
irb(main):007:1* 1/0
irb(main):008:1> rescue ZeroDivisionError
irb(main):009:1> p $!.class
irb(main):010:1> raise
irb(main):011:1> end
ZeroDivisionError
ZeroDivisionError: divided by 0
    from (irb):7:in `/'
  from (irb):7
  from /Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):012:0> a = 0
=> 0
irb(main):013:0> begin
irb(main):014:1* b = 1 / a
irb(main):015:1> rescue ZeroDivisionError
irb(main):016:1> a += 1
irb(main):017:1> retry
irb(main):018:1> ensure
irb(main):019:1* p b
irb(main):020:1> end
1
=> 1
irb(main):021:0> begin
irb(main):022:1* 1/0
irb(main):023:1> rescue
irb(main):024:1> p 1
irb(main):025:1> rescue ZeroDivisionError
irb(main):026:1> p 2
irb(main):027:1> end
1
=> 1
irb(main):028:0> def foo
irb(main):029:1> throw :exit
irb(main):030:1> end
=> :foo
irb(main):031:0> catch(:exit){
irb(main):032:1* foo
irb(main):033:1> p 1
irb(main):034:1> }
=> nil
irb(main):035:0> p 2
2
=> 2
irb(main):036:0> def bar
irb(main):037:1> catch(:calc) do
irb(main):038:2* throw :calc, 100
irb(main):039:2> end
irb(main):040:1> end
=> :bar
irb(main):041:0> p bar
100
=> 100
irb(main):042:0> exit

例外処理、しっかりしないとですね。