[#21736] OBJECTDAY 2000 — 助田 雅紀 <masaki.suketa@...>

助田です.

29 messages 2000/04/03
[#21748] Re: OBJECTDAY 2000 — keiju@... (石塚圭樹) 2000/04/04

けいじゅ@日本ラショナルソフトウェアです.

[#21753] Re: OBJECTDAY 2000 — Hideto ISHIBASHI <hideto-i@...4u.or.jp> 2000/04/05

石橋秀仁です。

[#21755] Re: OBJECTDAY 2000 — keiju@... (石塚圭樹) 2000/04/05

けいじゅ@日本ラショナルソフトウェアです.

[#21837] site_ruby & Linux distribution — OZAWA Sakuro <crouton@...>

小澤さくです。

14 messages 2000/04/10

[#21922] ruby-1.4.4 binary for BeOS 5 (Intel) — tenmei@... (Noriaki Harada)

はらだです。

23 messages 2000/04/15
[#21923] Re: ruby-1.4.4 binary for BeOS 5 (Intel) — NAITOH Jun <naitoh@...> 2000/04/15

はじめまして、内藤と申します。

[#21926] Re: ruby-1.4.4 binary for BeOS 5 (Intel) — tenmei@... (Noriaki Harada) 2000/04/16

>はじめまして、内藤と申します。

[#21930] Re: ruby-1.4.4 binary for BeOS 5 (Intel) — NAITOH Jun <naitoh@...> 2000/04/16

内藤です。

[#21968] Re: ruby-1.4.4 binary for BeOS 5 (Intel) — Katsuyuki Komatsu <komatsu@...> 2000/04/17

小松です。

[#21969] Re: ruby-1.4.4 binary for BeOS 5 (Intel) — NAITOH Jun <naitoh@...> 2000/04/17

再び内藤です。

[#21956] Thread & STDIN — Masato Toyoshima <mt@...>

はじめまして、豊島といいます。

16 messages 2000/04/17

[#21981] rd2man — WATANABE Hirofumi <eban@...>

わたなべです.

29 messages 2000/04/18
[#21982] Re: rd2man — Toshiro Kuwabara <toshirok@...3.so-net.ne.jp> 2000/04/18

Toshです。

[#21985] Re: rd2man — WATANABE Hirofumi <Hirofumi.Watanabe@...> 2000/04/19

わたなべです.

[#22036] Re: rd2man — Wakou Aoyama <wakou@...> 2000/04/20

青山です。

[#22040] Re: rd2man — Toshiro Kuwabara <toshirok@...3.so-net.ne.jp> 2000/04/21

Toshです。

[#22014] shell interactive — WATANABE Daisaku <dwata@...3.rim.or.jp>

31 messages 2000/04/20
[#22016] Re: shell interactive — 時田幸粋 <ktokita@...> 2000/04/20

こんにちは、時田です。

[#22017] Re: shell interactive — nobu.nakada@... 2000/04/20

なかだです。

[#22018] Re: shell interactive — WATANABE Hirofumi <Hirofumi.Watanabe@...> 2000/04/20

わたなべです.

[#22027] Re: shell interactive — 時田幸粋 <ktokita@...> 2000/04/20

こんにちは、時田です。

[#22103] Please put ruby-1.4.4.tar.gz in pub/lang/ruby/1.4/ — "Akinori -Aki- MUSHA" <knu@...>

 お願いがあるのですが、 ruby-1.4.4.tar.gz を FTP ツリーの 1.4/

16 messages 2000/04/25
[#22199] Where to put ruby-i.j.k.tar.gz on? (Re: ) — SUGIHARA Hiroshi <maili31s@...> 2000/04/28

[ruby-list:22103]におきまして、2000/04/25 12:38ほどに、

[#22117] StateGraph: state machine interpreter — Hideto ISHIBASHI <hideto-i@...4u.or.jp>

石橋 "rubyholic" 秀仁です。

16 messages 2000/04/25

[#22159] $LOAD_PATH への追加について — "Sugawara Hajime" <sugawara@...>

 はじめまして、菅原@ホビー・データです。

13 messages 2000/04/27

[#22181] Can't Use optparse — Yamada Kenji <kyamada@...>

28 messages 2000/04/28
[#22186] Re: Can't Use optparse — akira yamada / やまだあきら <akira@...> 2000/04/28

[#22195] Re: Can't Use optparse — Toshiro Kuwabara <toshirok@...3.so-net.ne.jp> 2000/04/28

Toshです。

[#22261] Re: Can't Use optparse — nobu.nakada@... 2000/05/04

なかだです。

[#22264] Re: Can't Use optparse — Toshiro Kuwabara <toshirok@...3.so-net.ne.jp> 2000/05/04

Toshです。

[#22270] Re: Can't Use optparse — nobu.nakada@... 2000/05/04

なかだです。

[#22272] Re: Can't Use optparse — Toshiro Kuwabara <toshirok@...3.so-net.ne.jp> 2000/05/04

Toshです。

[#22300] Re: Can't Use optparse — Koji Arai <JCA02266@...> 2000/05/05

新井です。

[ruby-list:22066] Re: assert

From: Hideto ISHIBASHI <hideto-i@...4u.or.jp>
Date: 2000-04-22 10:49:50 UTC
List: ruby-list #22066
石橋秀仁です。

> 友谷です。

> で、assertを組み込むのであればこんなのはどうでしょうか。

ちょっと脱線します。
厳密にはassertではないのですが、次のようなものはどうでしょう?
実行時にエラーの回復をするためのライブラリです。

--
Hideto "rubyholic" ISHIBASHI

Attachments (2)

assert.rb (1.95 KB, text/x-ruby)
module Assert

  class AssertionError < StandardError; end

  def assert(*args, &block)
    return nil unless $DEBUG
    __assert__(*args, &block)
  end

  def __assert__(*args)
    return true if yield == true
    msg = "assertion failed:"
    for arg in args
      msg += " " + arg.inspect
    end
    raise AssertionError, msg
  end

  def recoverable_assert(*args, &block)
    return nil unless $DEBUG
    assert_and_recovery(*args, &block)
  end

  def assert_and_recovery(*args, &block)
    begin
      return true if __assert__(*args, &block)
    rescue
      exception = $!
      STDOUT.print exception, "\n"
      for c in caller(0)
        STDOUT.print c, "\n"
      end
    end
    STDOUT.print "type 'help' to display help message.\n"

    loop do
      input = readline("(recovery:-) ")
      case input
      when /^\s*h(?:elp)?\s*$/
	STDOUT.print <<EOS
help	print this message
fail	raise AssertionError
local	print local variables
retry	retry assertion
...	eval an input string
EOS
      when /^\s*f(?:ail)?\s*$/
	fail exception
      when /^\s*l(?:ocal)?\s*$/
        STDOUT.print "local variables (name : type = value)\n"
	eval(<<-EOE, block)
          for lv in local_variables - ["lv"]
            STDOUT.print " ", lv, "\t: ", lv.type, "\t= ", eval(lv), "\n"
          end
	EOE
      when /^\s*r(?:etry)?\s*$/
        begin
          return true if __assert__(*args, &block)
        rescue
          STDOUT.print $!, "\n"
        end
      else
	begin
          STDOUT.print eval(input, block).inspect, "\n"
	rescue
	  STDOUT.print $!, "\n"
	end
      end
    end
  end

  def readline(prompt)
    STDOUT.print prompt
    STDOUT.flush
    line = STDIN.gets
    line.chomp!
    line
  end
end

if __FILE__ == $0

  include Assert

  orig = "This is a pen."
  pen = orig.dup
  str = pen

  assert("pen == orig ?") {pen == orig}
  str.sub!(/a/, "A")
  recoverable_assert("pen == orig ?") {pen == orig}
  #assert_and_recovery("pen == orig ?") {pen == orig}

  print "recoverd.\n"

end
screenshot.txt (1.5 KB, text/plain)
$ ruby assert.rb 
recoverd.
$ ruby -d assert.rb 
Exception `Assert::AssertionError' at assert.rb:16
assertion failed: "pen == orig ?"
assert.rb:30:in `assert_and_recovery'
assert.rb:21:in `recoverable_assert'
assert.rb:91
type 'help' to display help message.
(recovery:-) help
help	print this message
fail	raise AssertionError
local	print local variables
retry	retry assertion
...	eval an input string
(recovery:-) l
local variables (name : type = value)
 orig	: String	= This is a pen.
 pen	: String	= This is A pen.
 str	: String	= This is A pen.
(recovery:-) r
Exception `Assert::AssertionError' at assert.rb:16
assertion failed: "pen == orig ?"
(recovery:-) pen.sub!(/A/, "a")
"This is a pen."
(recovery:-) l
local variables (name : type = value)
 orig	: String	= This is a pen.
 pen	: String	= This is a pen.
 str	: String	= This is a pen.
(recovery:-) orig
"This is a pen."
(recovery:-) retry
recoverd.
$ ruby -d assert.rb 
Exception `Assert::AssertionError' at assert.rb:16
assertion failed: "pen == orig ?"
assert.rb:30:in `assert_and_recovery'
assert.rb:21:in `recoverable_assert'
assert.rb:91
type 'help' to display help message.
(recovery:-) r
Exception `Assert::AssertionError' at assert.rb:16
assertion failed: "pen == orig ?"
(recovery:-) f
Exception `Assert::AssertionError' at assert.rb:48
assert.rb:48:in `assert_and_recovery': assertion failed: "pen == orig ?" (Assert::AssertionError)
	from assert.rb:37:in `loop'
	from assert.rb:37:in `assert_and_recovery'
	from assert.rb:21:in `recoverable_assert'
	from assert.rb:91
$ 

In This Thread