Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stormbrew/289298 to your computer and use it in GitHub Desktop.
Save stormbrew/289298 to your computer and use it in GitHub Desktop.
From a3f952616a416480f9f382248c39d0f37b1385b2 Mon Sep 17 00:00:00 2001
From: Graham <graham-storm-git@ript.net>
Date: Thu, 28 Jan 2010 15:46:33 -0700
Subject: [PATCH 1/2] Updated spec for issue when raising an object instance with a message
In MRI, the internal behaviour when passed an object instance as the first argument and a message as the second argument to raise is to clone the object and call the internal (original) initialize on the object. This makes it so that message is set even if the derived initialize doesn't take a message argument.'
---
spec/ruby/language/rescue_spec.rb | 23 +++++++++++++++++++++++
1 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/spec/ruby/language/rescue_spec.rb b/spec/ruby/language/rescue_spec.rb
index 2bd01eb..1b1bd11 100644
--- a/spec/ruby/language/rescue_spec.rb
+++ b/spec/ruby/language/rescue_spec.rb
@@ -3,6 +3,12 @@ class SpecificExampleException < StandardError
end
class OtherCustomException < StandardError
end
+class CustomArgumentException < StandardError
+ attr_reader :val
+ def initialize(val)
+ @val = val
+ end
+end
def exception_list
[SpecificExampleException, ZeroDivisionError]
@@ -24,6 +30,23 @@ describe "The rescue keyword" do
e.message.should == "some text"
end
end
+
+ it "can take an object instance instead of a class to raise" do
+ begin
+ raise CustomArgumentException.new(1)
+ rescue CustomArgumentException => e
+ e.val.should == 1
+ end
+ end
+
+ it "can take an object instance instead of a class to raise as well as a message" do
+ begin
+ raise CustomArgumentException.new(1), "some text"
+ rescue CustomArgumentException => e
+ e.val.should == 1
+ e.message.should == "some text"
+ end
+ end
it "can rescue multiple raised exceptions with a single rescue block" do
lambda do
--
1.6.1.2
From 0d337d98ffbd85baa49c5a022a66863a0ab86f1c Mon Sep 17 00:00:00 2001
From: Graham <graham-storm-git@ript.net>
Date: Thu, 28 Jan 2010 16:28:31 -0700
Subject: [PATCH 2/2] Fix message behaviour when raising an exception to match MRI.
Made it so that when raising with both an instance and a message, Exception#exception calls the original initialize method instead of one that may have been overridden by a derived class. This ensures that Exception#message gives the right result even if a different initialization has been performed.
---
kernel/common/exception.rb | 5 ++++-
1 files changed, 4 insertions(+), 1 deletions(-)
diff --git a/kernel/common/exception.rb b/kernel/common/exception.rb
index 0b6a445..15aee27 100644
--- a/kernel/common/exception.rb
+++ b/kernel/common/exception.rb
@@ -9,6 +9,7 @@ class Exception
@locations = nil
@backtrace = nil
end
+ alias_method :__initialize__, :initialize
def backtrace
if @backtrace
@@ -77,7 +78,9 @@ class Exception
def exception(message=nil)
if message
unless message.equal? self
- return self.class.new(message)
+ e = self.clone
+ e.__initialize__(message)
+ return e
end
end
--
1.6.1.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment