[前][次][番号順一覧][スレッド一覧]

ruby-changes:44016

From: nobu <ko1@a...>
Date: Wed, 7 Sep 2016 17:22:00 +0900 (JST)
Subject: [ruby-changes:44016] nobu:r56089 (trunk): timeout.rb: custom error message

nobu	2016-09-07 17:21:56 +0900 (Wed, 07 Sep 2016)

  New Revision: 56089

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56089

  Log:
    timeout.rb: custom error message
    
    * lib/timeout.rb (Timeout#timeout): add custom error message
      argument.  [Feature #11650]

  Modified files:
    trunk/ChangeLog
    trunk/lib/timeout.rb
    trunk/test/test_timeout.rb
Index: test/test_timeout.rb
===================================================================
--- test/test_timeout.rb	(revision 56088)
+++ test/test_timeout.rb	(revision 56089)
@@ -66,6 +66,11 @@ class TestTimeout < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_timeout.rb#L66
         sleep 3
       end
     end
+    assert_raise_with_message(err, /connection to rubylang.org expired/) do
+      Timeout.timeout 0.01, err, "connection to rubylang.org expired" do
+        sleep 3
+      end
+    end
   end
 
   def test_exit_exception
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56088)
+++ ChangeLog	(revision 56089)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Sep  7 17:21:55 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/timeout.rb (Timeout#timeout): add custom error message
+	  argument.  [Feature #11650]
+
 Wed Sep  7 17:13:05 2016  Martin Duerst  <duerst@i...>
 
 	* common.mk: Updated Unicode version to 9.0.0 [Feature #12513]
Index: lib/timeout.rb
===================================================================
--- lib/timeout.rb	(revision 56088)
+++ lib/timeout.rb	(revision 56089)
@@ -60,6 +60,8 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L60
   #         value of 0 or +nil+ will execute the block without any timeout.
   # +klass+:: Exception Class to raise if the block fails to terminate
   #           in +sec+ seconds.  Omitting will use the default, Timeout::Error
+  # +message+:: Error message to rasise with Exception Class.
+  #             Omitting will use the default, "execution expired"
   #
   # Returns the result of the block *if* the block completed before
   # +sec+ seconds, otherwise throws an exception, based on the value of +klass+.
@@ -70,9 +72,9 @@ module Timeout https://github.com/ruby/ruby/blob/trunk/lib/timeout.rb#L72
   # Note that this is both a method of module Timeout, so you can <tt>include
   # Timeout</tt> into your classes so they have a #timeout method, as well as
   # a module method, so you can call it directly as Timeout.timeout().
-  def timeout(sec, klass = nil)   #:yield: +sec+
+  def timeout(sec, klass = nil, message = nil)   #:yield: +sec+
     return yield(sec) if sec == nil or sec.zero?
-    message = "execution expired".freeze
+    message ||= "execution expired".freeze
     from = "from #{caller_locations(1, 1)[0]}" if $DEBUG
     e = Error
     bl = proc do |exception|

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]